哈希火箭是否被弃用? [英] Is Hash Rocket deprecated?
问题描述
引用广泛 RIP Hash火箭文章似乎暗示了Hash Rocket语法(:foo =>bar
)已被弃用,以支持新的 - 到 - Ruby JSON风格的散列( foo:bar
),但我找不到任何明确的引用,声明Hash Rocket实际上已被弃用/ Ruby 1.9。
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
有效,但'where.is':x
不是。 Ruby 2.2已经解决了这个问题,所以你可以在Ruby 2.2 +中说'where.is':x
。 - 必须使用对于无效标签符号的火箭:
:$ set => x
是有效的,但$ set:x
不是。在Ruby 2.2中,你可以用引号解决这个问题:'$ set':x
会做正确的事情。 - 你如果您在Hashes中使用不是符号的键,则必须使用火箭:
's'=> x
是有效的,但's':x
是完全不同的。
- You must use the rocket for symbols that require quoting:
:'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+. - 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. - You must use the rocket if you use keys in your Hashes that aren't symbols:
'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屋!