问题
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.
回答1:
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 require quoting:
:'where.is' => xis valid but'where.is': xis not. Ruby 2.2 has fixed this problem so you can say'where.is': xin Ruby 2.2+. - You must use the rocket for symbols that are not valid labels:
:$set => xis valid but$set: xis not. In Ruby 2.2+ you can get around this problem with quotes:'$set': xwill do The Right Thing. - You must use the rocket if you use keys in your Hashes that aren't symbols:
's' => xis valid but's': xis 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.
来源:https://stackoverflow.com/questions/10004158/is-hash-rocket-deprecated