Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this format in ruby?

Tags:

syntax

ruby

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?

like image 554
never_had_a_name Avatar asked Dec 13 '22 20:12

never_had_a_name


2 Answers

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" }
like image 121
Chuck Avatar answered Dec 31 '22 01:12

Chuck


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

like image 40
sepp2k Avatar answered Dec 31 '22 02:12

sepp2k