Hash Rocket 是否已被废弃?

被广泛引用的 安息吧,哈希火箭柱似乎意味着 Hash Rocket 语法(:foo => "bar")已经不被支持新到 Ruby JSON 风格的 Hash (foo: "bar")了,但是我找不到任何明确的参考资料来说明 Hash Rocket 形式实际上已经被废弃/不建议使用 Ruby 1.9了。

35595 次浏览

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 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.

  2. 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.