Is Hash Rocket deprecated?

Go To StackoverFlow.com

95

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.

2012-04-04 02:23
by mahemoff
I think that guy only uses Ruby with Rails - Niklas B. 2012-04-04 02:57


128

The author of that blog post is being overly dramatic and foolish, the => is still quite necessary. In particular:

  1. You must use the rocket for symbols that require quoting: :'where.is' => x is valid but 'where.is': x is not. Ruby 2.2 has fixed this problem so you can say 'where.is': x in Ruby 2.2+.
  2. 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.
  3. You must use the rocket if you use keys in your Hashes that aren't symbols: '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.

2012-04-04 02:54
by mu is too short
s/overly dramatic and foolish/dramatic and advocational with an eloquent homage/. The rest of your points stand - dbenhur 2012-04-04 05:47
I agree, it's certainly one of the most eloquent posts about a language update. Albeit a little misleading :D - mahemoff 2012-04-04 11:54
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 - Dave Rapin 2012-04-18 23:59
@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 - mu is too short 2012-04-19 00:05
@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 - Phrogz 2012-05-31 20:12
What should be used on this: { my_object.name => 'value' } - fabriciofreitag 2016-07-01 07:27
@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 - mu is too short 2016-07-01 16:39
This is what I wanted to know, thanks - fabriciofreitag 2016-07-01 23:05
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 - Huliax 2017-04-30 10:19
@Huliax 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 - mu is too short 2017-04-30 17:32
Ads