我有一个像这样的元素
customers = [{ name: 'John Doe', age: 30, height: 175 }, { name: 'John Doe', age: 30, height: 175 } ... etc]
我知道这是一个简单的问题,但我怎样才能为这个元素添加另一个客户。即。
{name: 'Jane' , age: 25, height 150 }
提前致谢!
答案 0 :(得分:3)
使用shovel
方法:
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}]