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.
精彩评论