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

后端 未结 5 2021
有刺的猬
有刺的猬 2020-11-22 02:24

Is there any difference between :key => \"value\" (hashrocket) and key: \"value\" (Ruby 1.9) notations?

If not, then I would like to use

5条回答
  •  青春惊慌失措
    2020-11-22 02:39

    Doing :key => value is the same as doing key: value, and is really just a convenience. I haven't seen other languages that use the =>, but others like Javascript use the key: value in their Hash-equivalent datatypes.

    As for a gem to convert the way you wrote out your hashes, I would just stick with the way you are doing it for your current project.

    *Note that in using key: value the key will be a symbol, and to access the value stored at that key in a foo hash would still be foo[:key].

提交回复
热议问题