Ad

Accessing Specific Attribute Of An Object

- 1 answer

Hello I have the following object

object =  [#<ShopifyAPI::DiscountCode:0x000000000e1c78a8 @attributes={"code"=>"Disc2", "amount"=>"1.00", "type"=>"percentage"}, @prefix_options={}, @persisted=true>]

How can I properly access the "code" name of that object?

I have tried object[:code] and object.code but it appears I am overlooking something.

Ad

Answer

object is an array of ShopifyAPI::DiscountCode. The best way to access it is

object[0].attributes['code']

If u want code of all the objects available in the array, you could get the array of values by

object.map { |obj| obj.attributes['code'] }
Ad
source: stackoverflow.com
Ad