Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 322 Vote(s) - 3.38 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ruby hash equivalent of JavaScript's object initializer ES6 shorthand

#1
In JavaScript ES6 we can create objects where variable names become keys like this:

> let a = 'aaa'
'aaa'

> let b = 'bbb'
'bbb'

> { a, b }
{ a:"aaa", b:"bbb" }

Does Ruby have something equivalent for hashes?

Clarification:
Obviously this question regards the shorthand notation. I'm looking for `{a,b}` not `{a:a,b:b}`.
Reply

#2
Starting from ruby 3.1, it can be done like this:

```rb
x = 1
y = 2

{x:, y:}
# => {:x=>1, :y=>2}
```

Values can be omitted for functions too:

```rb
def x
1
end

def y
2
end

{x:, y:}
# => {:x=>1, :y=>2}
```

[Playground](

[To see links please register here]

)
Reply

#3
Update: will be in 3.1

[To see links please register here]




No, there is no such shorthand notation.


Related proposals:
[#11105][1]
[#13137][2]

> Rejected. I saw JavaScript new syntax, but I had no sympathy. It doesn't make anything more understandable.
>
> Matz.

[1]:

[To see links please register here]

[2]:

[To see links please register here]



Reply

#4
Not built in to the language. But what do you think of this?




def hash_shorthand(source_binding, *symbols)
hash = {}

symbols.each do |symbol|
hash[symbol] = source_binding.local_variable_get(symbol)
end

hash
end

----

$ irb -r './hash_shorthand.rb'
>> x = 10
>> y = 20
>>
>> puts hash_shorthand(binding, :x, :y)
{:x=>10, :y=>20}

Only downside is that you'll need to pass the binding to get access to the local variables.
Reply

#5
It has been proposed in [Ruby #15236 - Add support for hash shorthand](

[To see links please register here]

) and has unfortunately been rejected for the moment.
Reply

#6
Although Ruby / Rails doesn't yet support an equivalent to the ES6 shorthand syntax for hashes, there are a few handy idioms already that often come in handy.

Ruby
====
Consider the following method:

def test_splat(a:, b:, c:)
[a, b, c].inspect
end
`test_splat(a: 4, b: 5, c: 6)` yields `"[4, 5, 6]"`

Although if you already have a hash such as `hash = { a: 1, b: 2, c: 3 }`, you can simply call it like this:

`test_splat(hash)` which yields `"[1, 2, 3]"`

### Further ###
If you have a sub_hash, you can use it alongside other kwargs using the kwarg splat operator `**`. For example if you have `sub_hash = { a: 1, b: 2 }`, calling:

`test_splat(sub_hash)` yields `ArgumentError: missing keyword: c`

and calling:

`test_splat(sub_hash, c: 3)` yields `ArgumentError: wrong number of arguments (given 1, expected 0)`

but using the splat operator `**`, you can do splat the `sub_hash` arg:

`test_splat(**sub_hash, c: 3)` which yields `"[1, 2, 3]"`

For more reading see

[To see links please register here]


Rails
=====

The above plus a few extra methods can come in handy for Rails users, particularly in controllers when params are passed in.

Suppose you have an `ActionController::Parameters` object with more attributes than you need and you want a subset. E.g: `{ a: 1, b: 2, d: 4 }`. The `slice` method on `Hash` is very handy here.

First, permit your params:

`permitted_params = params.permit(:a, :b, :d).to_h.symbolize_keys`.

We add `.to_h.symbolize_keys` because `ActionController::Parameters` doesn't support `symbolize_keys`, and kwargs require the args' keys to be symbols, not strings. So the `.to_h` converts it to an `ActiveSupport::HashWithIndifferentAccess`, and the `symbolize_keys` converts the hash's keys from strings to symbols.

Now, calling:

`test_splat(**permitted_params, c: 3)` will yield `ArgumentError: unknown keyword: d` as expected since `d` isn't a kwarg for the `test_splat` method. Although using slice achieves what we want here:

`test_splat(**permitted_params.slice(:a, :b), c: 3)` yields `"[1, 2, 3]"`

Reply

#7
**Short answer** no.

**Longer answer**

Shugo Maeda proposed a patch for this in 2015 (you can read the details about this here:

[To see links please register here]

).

At the time Matz wasn't into the idea, but might be willing to change his mind in the future.

In the mean time - you can make use of Shugo's patch and patch your own version of Ruby to have ES6 hash literals yourself!

To patch Ruby to add the hashes do the following:

1) Download the patch from here (currently works with Ruby 2.5.0)

2) Use RVM to install a patched version of this Ruby. i.e.

rvm install 2.5.0 -n imphash --patch imphash.patch

Then you can use RVM to select the patched version of Ruby:

rvm use 2.5.0-imphash

(Imphash is short for implicit hash)

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through