Jaison Brooks
Jaison Brooks

Reputation: 5826

Using variables returned in object in ruby

Im new to ruby and need to know how to easily display the specific variables in this returned response below.

Any help is greatly appreciated.

response

[#<Fedex::Rate:0x007fb9320ba7f0 @service_type="FEDEX_GROUND", 
  @rate_type="PAYOR_ACCOUNT_PACKAGE", @rate_zone="4", @total_billing_weight="6.0 LB", 
  @total_freight_discounts={:currency=>"USD", :amount=>"2.75"}, @total_net_charge="6.84", 
  @total_taxes="0.0", @total_net_freight="6.42", @total_surcharges="0.42", 
  @total_base_charge="9.17", @total_net_fedex_charge=nil, @total_rebates="0.0">]

Upvotes: 1

Views: 51

Answers (2)

Mark Thomas
Mark Thomas

Reputation: 37517

According to the docs, you call it this way:

rate = fedex.rate(:shipper=>shipper,
                  :recipient => recipient,
                  :packages => packages,
                  :service_type => "FEDEX_GROUND",
                  :shipping_options => shipping_options)

and then any of the instance variables (the @ variables) can be retrieved via an accessor:

puts rate.total_net_charge

Upvotes: 1

Pierre Pretorius
Pierre Pretorius

Reputation: 2909

It looks like the response is an array with one object of type Fedex::Rate. As mentioned by others, you should read the docs to see what Fedex::Rate exposes as methods. To programatically see the methods Fedex::Rate exposes you could use:

rate = response.first
puts rate.methods

Alternatively if you want to read the variables from the object (which is probably a bad idea), you could use:

rate.instance_variable_get('@total_freight_discounts')

Upvotes: 1

Related Questions