Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何访问数组中的实例变量?_Ruby On Rails_Arrays_Ruby - Fatal编程技术网

Ruby on rails 如何访问数组中的实例变量?

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

我有一个gem,它的输出具有以下数组(调用
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)