哈希火箭被弃用了吗? [英] Is Hash Rocket deprecated?
问题描述
被广泛引用的RIP Hash Rocket 帖子 似乎暗示 Hash Rocket 语法(:foo => "bar"
)已被弃用,以支持新的 Ruby JSON-style hash (foo: "bar"
),但我找不到任何明确的参考,说明自 Ruby 1.9 起,Hash Rocket 形式实际上已被弃用/不建议使用.
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:
对于不是有效标签的符号,您必须使用火箭:
:$set =>x
有效但$set: x
无效.在 Ruby 2.2+ 中,您可以使用引号解决这个问题:'$set': x
will do The Right Thing.
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.
如果您在哈希中使用不是符号的键,例如字符串、整数或常量,则必须使用火箭.例如,'s' =>x
是有效的,但 's': x
是完全不同的东西.
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.
如果不破坏 Ruby 的哈希值,火箭就不会去任何地方.
The rocket isn't going anywhere without crippling Ruby's Hashes.
这篇关于哈希火箭被弃用了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!