Ruby 1.9在密钥中带有短划线 [英] Ruby 1.9 hash with a dash in a key
本文介绍了Ruby 1.9在密钥中带有短划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
irb> {a:2}
$
=> {:a => 2}
irb> {ab:2}
SyntaxError:(irb):5:语法错误,意外的tLABEL
{ab:2}
^
与旧的,它的工作:
irb> {:a-b=> 2}
=> {:ab=> 2}
解决方案
{a:1,b:2,'c-c':3,d:4}也可以使用下一个语法
In ruby 1.9 is there a way to define this hash with the new syntax?
irb> { a: 2 } => {:a=>2} irb> { a-b: 2 } SyntaxError: (irb):5: syntax error, unexpected tLABEL { a-b: 2 } ^
with the old one, it's working:
irb> { :"a-b" => 2 } => {:"a-b"=>2}
解决方案You also can use next syntax
{a: 1, b: 2, 'c-c': 3, d: 4}
这篇关于Ruby 1.9在密钥中带有短划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文