首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby数组属性

Ruby数组属性
EN

Stack Overflow用户
提问于 2016-07-18 23:53:20
回答 2查看 143关注 0票数 0

我正在尝试使用shopify-api gem访问Shopify API。特别是,我正在尝试访问一个商品的价格,它包含在一个数组中。

代码语言:javascript
复制
product = ShopifyAPI::Product.find(id)
variant = product.variants

这里的variant是一个数组,当我执行puts variant.inspect时,我得到

代码语言:javascript
复制
[
  #<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‘?

EN

回答 2

Stack Overflow用户

发布于 2016-07-19 00:00:59

属性是Ruby所说的对象属性。只需像访问大多数其他语言中的属性一样访问它。

获取所需的数组项(在本例中为0),然后访问价格属性

variant[0].price

票数 0
EN

Stack Overflow用户

发布于 2016-07-19 03:57:57

使用:

variant.first["price"]

variant数组有3个元素;@attributes元素是第一个。这个@attributes元素是一个散列映射,您要在key "price"处查找该值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38441127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档