chris cozzens
chris cozzens

Reputation: 517

Array of Hashes, how to add new element?

I have an element like this

customers =  [{ name: 'John Doe', age: 30, height: 175 }, { name: 'John Doe', age: 30, height: 175 } ... etc]

I know that this is a simple question, but how can I add another customer to this element. ie.

{name: 'Jane' , age: 25, height 150 } 

Thanks in advance!

Upvotes: 0

Views: 47

Answers (1)

jvillian
jvillian

Reputation: 20263

Use the shovel method:

customers =  [{ name: 'John Doe', age: 30, height: 175 }, { name: 'John Doe', age: 30, height: 175 }]
new_customer = {name: 'Jane' , age: 25, height: 150 } 
customers << new_customer
 => [{:name=>"John Doe", :age=>30, :height=>175}, {:name=>"John Doe", :age=>30, :height=>175}, {:name=>"Jane", :age=>25, :height=>150}]

Upvotes: 3

Related Questions