开发者

What is this format in ruby?

开发者 https://www.devze.com 2023-01-10 02:52 出处:网络
In a book they showed me this declaration: friends = [ { first_name: \"Emily\", last_name: \"Laskin\" }, { first_name: \"Nick\", last_name: \"Mauro\" }, { first_name: \"Mark\", last_name: \"Maxwell\"

In a book they showed me this declaration:

friends = [ { first_name: "Emily", last_name: "Laskin" }, { first_name: "Nick", last_name: "Mauro" }, { first_name: "Mark", last_name: "Maxwell" } ]

This doesn't look like a hash. And when I enter it in IRB i get 开发者_运维百科an error.

What is this format?


It's an array of hashes, written in the Ruby 1.9 hash syntax.

{ first_name: "Emily", last_name: "Laskin" }

is equivalent to:

{ :first_name => "Emily", :last_name => "Laskin" }


The {key: value} syntax is new in 1.9 and is equivalent to {:key => value}.


It is an array of hashes, only the hashes are 1.9 style hashes.

0

精彩评论

暂无评论...
验证码 换一张
取 消