如何使用新的哈希语法?

巴斯蒂安

我正在尝试使用新的哈希语法,但是它不起作用。我究竟做错了什么?

2.6.3 :151 > hash = { duplex: duplex}
 => {:duplex=>[#<Nokogiri::XML::Element:0x1e8ee04 name="duplex"....
2.6.3 :152 > hash["duplex"]
 => nil 
2.6.3 :153 > hash = { "duplex" => duplex}
 => {"duplex"=>[#<Nokogiri::XML::Element:0x1e8ee04 name="duplex" ....
2.6.3 :154 > hash["duplex"]
 => [#<Nokogiri::XML::Element:0x1e8ee04 name="duplex" ....
隆隆

“新”哈希语法用于用符号(:key)而不是字符串('key'"key"索引哈希因此,在您的情况下,请使用:

> hash = { duplex: duplex}
> hash[:duplex]
[#<Nokogiri::XML::Element:0x1e8ee04 name="duplex"...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章