Is Hash Rocket deprecated?
The author of that blog post is being overly dramatic and foolish, the =>
is still quite necessary. In particular:
-
You must use the rocket for symbols that are not valid labels:
:$set => x
is valid but$set: x
is not. In Ruby 2.2+ you can get around this problem with quotes:'$set': x
will do The Right Thing. -
You must use the rocket if you use keys in your Hashes that aren't symbols, such as strings, integers or constants. For example,
's' => x
is valid but's': x
is something completely different.
You can kludge around the above in the obvious manner of course:
h = { }
h[:'where.is'] = 'pancakes house?'
# etc.
but that's just ugly and unnecessary.
The rocket isn't going anywhere without crippling Ruby's Hashes.
Related videos on Youtube
mahemoff
Home http://mahemoff.com GitHub https://github.com/mahemoff Blog http://softwareas.com Twitter @mahemoff LinkedIn Mahemoff
Updated on July 14, 2022Comments
-
mahemoff almost 2 years
The well-cited RIP Hash rocket post would seem to imply the Hash Rocket syntax (
:foo => "bar"
) is deprecated in favor of the new-to-Ruby JSON-style hash (foo: "bar"
), but I can't find any definitive reference stating the Hash Rocket form is actually deprecated/unadvised as of Ruby 1.9.-
Niklas B. about 12 yearsI think that guy only uses Ruby with Rails.
-
Kermit almost 5 yearsLong live the rocket
-
mahemoff almost 5 yearsRocket is still going strong 🚀
-
-
dbenhur about 12 yearss/overly dramatic and foolish/dramatic and advocational with an eloquent homage/. The rest of your points stand.
-
mahemoff about 12 yearsI agree, it's certainly one of the most eloquent posts about a language update. Albeit a little misleading :D.
-
Dave Rapin about 12 yearsYou have to wonder if using the new syntax, when you still need to rely on the old syntax for certain scenarios, will simply complicate our code.
-
mu is too short about 12 years@DaveRapin: That's why I don't bother with the non-rocket syntax. I do a fair bit of MongoDB work and I often use non-symbols as Hash keys (never mind all the
h[:s]
I do) so the JavaScript style syntax is just pointless complication to me. Seems like a poorly thought out gee-whiz idea to me and now we're stuck with it and the related confusion forever. -
Phrogz about 12 years@DaveRapin Consider
a = [0,1,4,9]
vs.a = Array.new(4){ |i| i**2 }
. Why use the former when you sometimes need to use the latter? Answer: because it's more convenient. TIMTOWTDI does complicate the language, but this is a tradeoff. Lua is really elegant at the core and hence easy to learn, but annoying to actually code in. Ruby has a lot of special cases and custom features that make it harder to learn, but a joy to program in. I, for one, welcome the simpler-to-type, easier-to-read Hash-with-symbol-keys notation for the common case. -
fabriciofreitag almost 8 yearsWhat should be used on this: { my_object.name => 'value' } ?
-
mu is too short almost 8 years@fabriciofreitag: Not sure what you mean. If
my_object
is an object andname
is a method onmy_object
and you want the result ofmy_object.name
to be the key then{ my_object.name => 'value' }
is what you want. -
Huliax about 7 yearsWhile less fun to type, I definitely prefer the hash rocket. Why? because it means that any time I use a symbol for a key I can search for it anywhere in my project by searching for a string that starts with a colon. To me, the lack of consistency between the actual characters used to denote the key in
my_hash = {a:1}
andmyhash[:a] = 1
is, at the least, rather annoying. I'm sure I'm not the only who who feels this way. -
mu is too short about 7 years@Huliax I still use
=>
everywhere for your reason, because I use all kinds of things as hash keys, and becausea: :b
is both ugly and difficult to read. The whole "harder to type" argument seems silly to me when so many people use double quoted strings when they don't have to. So yeah, I'm with you on this one. It is interesting thatHash#inspect
still uses=>
for everything too.