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

后端 未结 3 1658
不思量自难忘°
不思量自难忘° 2021-02-02 07:16

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

3条回答
  •  既然无缘
    2021-02-02 07:24

    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.

提交回复
热议问题