我正在尝试使用shopify-api gem访问Shopify API。特别是,我正在尝试访问一个商品的价格,它包含在一个数组中。
product = ShopifyAPI::Product.find(id)
variant = product.variants这里的variant是一个数组,当我执行puts variant.inspect时,我得到
[
#<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>
]如何访问/更改'@attributes`下的'price‘?
发布于 2016-07-19 00:00:59
属性是Ruby所说的对象属性。只需像访问大多数其他语言中的属性一样访问它。
获取所需的数组项(在本例中为0),然后访问价格属性
variant[0].price
发布于 2016-07-19 03:57:57
使用:
variant.first["price"]
variant数组有3个元素;@attributes元素是第一个。这个@attributes元素是一个散列映射,您要在key "price"处查找该值。
https://stackoverflow.com/questions/38441127
复制相似问题