javascriptrubyhashecmascript-6shorthand

Ruby hash equivalent of JavaScript's object initializer ES6 shorthand


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}.


Solution

  • Update: will be in 3.1 https://bugs.ruby-lang.org/issues/14579#note-14

    No, there is no such shorthand notation.

    Related proposals: #11105 #13137

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

    Matz.