2016-07-18 4 views
0

Shopify APIには、shopify-api gemを使用してアクセスしようとしています。特に、配列に含まれるアイテムの価格にアクセスしようとしています。ここRuby配列の属性

product = ShopifyAPI::Product.find(id) 
variant = product.variants 

variantは配列で、私はputs variant.inspectを行うとき、私は、@のattributes`」下/変更に '価格' にアクセスするにはどうすればよい

[ 
    #<ShopifyAPI::Variant:0x000000041c9e50 @attributes={ 
    "id"=>23923477191, 
    "title"=>"Default Title", 
    "price"=>"6.00", 
    "sku"=>"shirts", 
    "position"=>1, 
    "grams"=>0, 
    "inventory_policy"=>"deny", 
    "compare_at_price"=>nil, 
    "fulfillment_service"=>"manual", 
    "inventory_management"=>"shopify", 
    "option1"=>"Default Title", 
    "option2"=>nil, 
    "option3"=>nil, 
    "created_at"=>"2016-06-30T14:06:07-04:00", 
    "updated_at"=>"2016-07-16T19:00:07-04:00", 
    "taxable"=>true, 
    "barcode"=>"", 
    "image_id"=>nil, 
    "inventory_quantity"=>1, 
    "weight"=>0.0, 
    "weight_unit"=>"kg", 
    "old_inventory_quantity"=>1, 
    "requires_shipping"=>true 
    }, @prefix_options={:product_id=>7509869767}, @persisted=true> 
] 

を取得しますか?

答えて

0

属性は、Rubyがオブジェクトプロパティと呼ぶものです。他の多くの言語のプロパティと同様にアクセスしてください。

したい配列(この例では0)の項目を取得し、その後、価格属性

variant[0].price

0

使用するアクセス:

variant.first["price"]

variant配列は、3つの要素を持っているが; @attributes要素が最初です。 この@attributes要素はハッシュマップであり、キー"price"の値を探しています。

関連する問題