hashrocket

Is there any difference between the `:key => “value”` and `key: “value”` hash notations?

点点圈 提交于 2019-11-25 22:27:53
问题 Is there any difference between :key => \"value\" (hashrocket) and key: \"value\" (Ruby 1.9) notations? If not, then I would like to use key: \"value\" notation. Is there a gem that helps me to convert from :x => to x: notations? 回答1: Yes, there is a difference. These are legal: h = { :$in => array } h = { :'a.b' => 'c' } h[:s] = 42 but these are not: h = { $in: array } h = { 'a.b': 'c' } # but this is okay in Ruby2.2+ h[s:] = 42 You can also use anything as a key with => so you can do this: