Ruby on rails 如何访问数组中的实例变量?
我有一个gem,它的输出具有以下数组(调用Ruby on rails 如何访问数组中的实例变量?,ruby-on-rails,arrays,ruby,Ruby On Rails,Arrays,Ruby,我有一个gem,它的输出具有以下数组(调用rate.inspect) 建议?您在rate中拥有的对象实际上是一个数组,包含一个元素,即一个Fedex::rate对象。可通过消息识别的: undefined method `total_net_charge' for #<Array:0x007f955408caf0> 太好了费率。首先。总净费用已完成。谢谢 undefined method `total_net_charge' for #<Array:0x007f955408c
rate.inspect
)
建议?您在
rate
中拥有的对象实际上是一个数组
,包含一个元素,即一个Fedex::rate
对象。可通过消息识别的:
undefined method `total_net_charge' for #<Array:0x007f955408caf0>
太好了<代码>费率。首先。总净费用已完成。谢谢
undefined method `total_net_charge' for #<Array:0x007f955408caf0>
undefined method `total_net_charge' for #<Array:0x007f955408caf0>
rate.first.total_net_charge
# Or by index
rate[0].total_net_charge
# Or assuming the array will sometimes have multiple objects
# loop or map to get them all
rate.each {|r| puts r.total_net_charge}
# or by map as an array of just the charges
rate.map(&:total_net_charge)