我想从散列数组中添加特定属性下面是一个数组示例:
@horses = [
{name: "Runner1", odds: 4.00},
{name: "Runner2", odds: 20.00},
{name: "Runner3", odds: 4.00}
]
我在尝试这个方法:
@total_odds = horses[:odds].inject(:+)
但我有个错误:
[ ]': can't convert Symbol into Integer
我做错什么了非常感谢(刚刚开始学习)
最佳答案
@horses.collect {|h| h[:odds] }.inject(:+)