1

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">]
7
  • what values you want to display ? Commented Mar 17, 2014 at 19:12
  • 1
    We can't help you based on the code you've posted. You need to figure out what public methods the Fedex::Rate class exposes. Commented Mar 17, 2014 at 19:14
  • @ArupRakshit Im just trying to display any of the @'s. Commented Mar 17, 2014 at 19:17
  • check if any getter method exist like #rate_type for the instances of the class Fedex::Rate.. Commented Mar 17, 2014 at 19:18
  • Do you have access to the source code of this object? If so paste that. It'll either be self explanatory or folks here can help you with code in front of them. Commented Mar 17, 2014 at 19:27

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Mark, appreciate the response. Im receiving a NoMethodError for total_net_charge by doing it this way. Any ideas?
It's not sufficient to know that it's a NoMethodError, you have to know what object it is being sent to. Please note the object in the error message.
The returned response is undefined method total_net_charge' for #<Array:0x007fc8db3499b8> (NoMethodError)`
For some reason, your response is being wrapped in an Array. I'd like to see your calling code. Does it match the examples on the gem's documentation page? Try rate.first.total_net_charge instead.
So if i had first to the call, it works!!! Thank you Mark. And yes it matches exactly what the doc states.
1

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')

2 Comments

Thanks @Pierre Pretorius! The returned methods are in my above updated description.
@JaisonBrooks That looks like a stacktrace, not a method list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.