ruby中的这种格式是什么?

时间:2022-12-22 22:30:13

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.

这看起来不像哈希。当我在IRB中输入它时,我得到一个错误。

What is this format?

这种格式是什么?

3 个解决方案

#1


5  

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

它是一个哈希数组,用Ruby 1.9哈希语法编写。

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

is equivalent to:

相当于:

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

#2


4  

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

{key:value}语法是1.9中的新语法,相当于{:key => value}。

#3


4  

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

它是一个哈希数组,只有哈希是1.9样式的哈希。

#1


5  

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

它是一个哈希数组,用Ruby 1.9哈希语法编写。

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

is equivalent to:

相当于:

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

#2


4  

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

{key:value}语法是1.9中的新语法,相当于{:key => value}。

#3


4  

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

它是一个哈希数组,只有哈希是1.9样式的哈希。