ruby 哈希火箭被弃用了吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10004158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Hash Rocket deprecated?
提问by mahemoff
The well-cited RIP Hash rocket postwould 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.
被广泛引用的RIP HashRocket帖子似乎暗示 Hash Rocket 语法 ( :foo => "bar") 已被弃用,以支持新的 Ruby JSON 样式哈希 ( foo: "bar"),但我找不到任何明确的参考资料说明 Hash Rocket 形式从 Ruby 1.9 开始,实际上已弃用/不建议使用。
回答by mu is too short
The author of that blog post is being overly dramatic and foolish, the =>is stillquitenecessary. 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.
- 对于需要引用的符号,您必须使用火箭:
:'where.is' => x有效但'where.is': x无效。Ruby 2.2 已经修复了这个问题,所以你可以'where.is': x在 Ruby 2.2+ 中说。 - 对于不是有效标签的符号,您必须使用火箭:
:$set => x有效但$set: x不是。在 Ruby 2.2+ 中,您可以使用引号解决这个问题:'$set': xwill do The Right Thing。 - 如果您在哈希中使用不是符号的键,则必须使用火箭:
's' => x有效但's': x完全不同的东西。
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.
如果不削弱 Ruby 的哈希值,火箭就不会去任何地方。

