Why is this string key in a hash converted to a symbol?

▼魔方 西西 提交于 2019-12-03 07:27:57

问题


Using Ruby 2.3:

In example 1, the string key "a" is automatically converted to a symbol, whereas with example 2, it stays a string.

Example 1

{"a": 1}
# => {:a=>1} 

Example 2

{"a"=>"c"}
# => {"a"=>"c"}

I thought : was the same as the old style hash rocket => syntax. What is going on? Why have I never noticed this in Rails? Is it the HashWithIndifferentAccess that is obscuring this?


回答1:


In Ruby 2.3(.0), these are all the same:

{:"a" => 1}
{"a": 1},
{:a => 1}
{a: 1} 

They all translate to the same thing: a is a symbol in all these cases.

{"a"=>1} is different: a is a string in this case.




回答2:


It's because of the new hash syntax introduced with ruby 1.9. The syntax with colon works with symbol keys only. It's called a "symbol to object" hash and it's only syntactic sugar for the most common style of hashes out there. Another point for me, it's closer to the javascript object notation.

If I have mixed key types then I prefer the old style (hash-rocket syntax), but that's up to you. Mixing the two style looks ugly to me.




回答3:


According to Ruby documentation:

Blockquote Symbol objects represent names and some strings inside the Ruby interpreter. They are generated using the :name and :"string" literals syntax, and by the various to_sym methods. [...]

This means that running:

$ ruby -e ruby -e "h = {key: \"value\"}; puts h"
$ ruby -e ruby -e "h = {:key => \"value\"}; puts h"
$ ruby -e ruby -e "h = {\"key\": \"value\"}; puts h"
$ ruby -e ruby -e "h = {:\"key\" => \"value\"}; puts h"
$ ruby -e ruby -e "h = {\"#{:key}\": \"value\"}; puts h"

Will produce the same result:

$ {:key=>"value"}


来源:https://stackoverflow.com/questions/36513996/why-is-this-string-key-in-a-hash-converted-to-a-symbol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!