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.
The author of that blog post is being overly dramatic and foolish, the =>
is still quite necessary. In particular:
:'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+.:$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.'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.
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
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
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
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
=>
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