我有一个带有以下数组的输出的gem(调用MainActivity
)
rate.inspect
我似乎无法弄清楚要在[#<Fedex::Rate:0x007f9552bd6200 @service_type="FEDEX_GROUND", @transit_time="TWO_DAYS", @rate_type="PAYOR_ACCOUNT_PACKAGE", @rate_zone="4", @total_billing_weight="8.0 LB", @total_freight_discounts={:currency=>"USD", :amount=>"0.0"}, @total_net_charge="18.92", @total_taxes="0.0", @total_net_freight="18.19", @total_surcharges="0.73", @total_base_charge="18.19", @total_net_fedex_charge=nil, @total_rebates="0.0">]
上调用什么来访问不同的值。我试过rate
,但我得到了:
rate.total_net_charge
么?
答案 0 :(得分:6)
rate
内的对象实际上是Array
,其中包含一个元素Fedex::Rate
对象。消息可以识别出这一点:
undefined method `total_net_charge' for #<Array:0x007f955408caf0>
更巧妙的是[]
对象周围的方括号<Fedex::Rate>
。因此,要深入检索total_net_charge
,您需要使用数组方法或索引:
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)