key in ruby new hash

后端 未结 2 817
深忆病人
深忆病人 2021-01-21 22:47

There is a weird issue with Ruby 1.9.2\'s new hash syntax. How can I put any Object as key in hash in 1.9.2?

in 1.8.7 hash it works:

a=\"b\" 
{\"a\" =>         


        
相关标签:
2条回答
  • 2021-01-21 22:56

    In Ruby 1.9 you are allowed to put colon : only after symbols that are used as keys!

    Any object can use the arrow =>, even symbols.

    0 讨论(0)
  • 2021-01-21 23:12

    To say this another way, the new feature isn't a new general hash syntax, it's a specific tweak for writing hashes where the keys are symbol literals. {a: 1} is just a shortcut for {:a => 1}, and that's all. If you have anything else as keys, you have to use the regular syntax.

    0 讨论(0)
提交回复
热议问题