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.
Is Hash Rocket deprecated?
I think that guy only uses Ruby with Rails. β
Bolero
Long live the rocket β
Fallible
Rocket is still going strong π β
Florrieflorry
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.
s/overly dramatic and foolish/dramatic and advocational with an eloquent homage/. The rest of your points stand. β
Tungusic
I agree, it's certainly one of the most eloquent posts about a language update. Albeit a little misleading :D. β
Florrieflorry
You 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. β
Hickie
@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. β
Mori @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. β
Staggers What should be used on this: { my_object.name => 'value' } ? β
Imminent
@fabriciofreitag: Not sure what you mean. If
my_object
is an object and name
is a method on my_object
and you want the result of my_object.name
to be the key then { my_object.name => 'value' }
is what you want. β
Mori While 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}
and myhash[:a] = 1
is, at the least, rather annoying. I'm sure I'm not the only who who feels this way. β
Fireproofing @Fireproofing I still use
=>
everywhere for your reason, because I use all kinds of things as hash keys, and because a: :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 that Hash#inspect
still uses =>
for everything too. β
Mori © 2022 - 2024 β McMap. All rights reserved.