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

后端 未结 5 2018
有刺的猬
有刺的猬 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:41

    Ruby hash-keys assigned by hash-rockets can facilitate strings for key-value pairs (e.g. 's' => x) whereas key assignment via symbols (e.g. key: "value" or :key => "value") cannot be assigned with strings. Although hash-rockets provide freedom and functionality for hash-tables, specifically allowing strings as keys, application performance may be slower than if the hash-tables were to be constructed with symbols as hash-keys. The following resources may be able to clarify any differences between hashrockets and symbols:

    • Ryan Sobol's Symbols in Ruby
    • Ruby Hashes Exaplained by Erik Trautman

提交回复
热议问题