Feature #13137
closedHash Shorthand
Description
The ES6 update to Javascript added object shorthand. I propose that a similar hash shorthand be added to Ruby.
Before¶
name = 'Matz'
country = 'Japan'
attributes = {
name: name,
country: country
}
After¶
name = 'Matz'
country = 'Japan'
attributes = {
name,
country
}
This would be very useful when building hashes from keyword arguments (especially with default values):
def build(name: 'John Doe', age: 100, country: 'Earth', )
attributes = {
name,
age,
country
}
end
Updated by znz (Kazuhiro NISHIYAMA) almost 8 years ago
- Related to Feature #11105: ES6-like hash literals added
Updated by shevegen (Robert A. Heiler) almost 8 years ago
While I understand the primary reasoning behind it - less to type - I do not like the syntax proposal.
To me it looks as if there is something missing in the build-up of the hash elements.
It appears to put load onto my brain and appears to be inconsistent with how I would normally populate a hash with key-value pairs.
Perhaps one day we could use some advanced hash that does all of these things including hash with indifferent syntax.
But I think it would be better for core hash to stay the way it is without any new implicit syntax.
To quote matz though, he wrote in the other thread that there may be a chance to revisit it in the future, e. g. when ES6 syntax may be more popular.
Updated by trevorlynnsmith (Trevor Smith) almost 8 years ago
My apologies – I searched extensively and did not find the previous feature request. Thank you for the consideration.
Updated by matz (Yukihiro Matsumoto) over 7 years ago
- Status changed from Open to Rejected
Rejected. I saw JavaScript new syntax, but I had no sympathy. It doesn't make anything more understandable.
Matz.
Updated by naruse (Yui NARUSE) over 7 years ago
Note: such culture seems to come from SGML.