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

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

    The key: value JSON-style assignments are a part of the new Ruby 1.9 hash syntax, so bear in mind that this syntax will not work with older versions of Ruby. Also, the keys are going to be symbols. If you can live with those two constraints, new hashes work just like the old hashes; there's no reason (other than style, perhaps) to convert them.

提交回复
热议问题