如何在数组中访问哈希?

时间:2011-04-26 20:11:09

标签: ruby-on-rails ruby

我正在尝试使用Nokogiri从XML文件中获取一些数据,然后将其保存到数据库中。

我正在使用的代码是:

def self.import_from_feed(feed)
 doc = Nokogiri::XML(open(feed))

 @products = doc.xpath('/merchantProductFeed/merchant/prod').map do |i| 
   {
     'name' => i.xpath('text/name').inner_text,
     'link' => i.xpath('uri/mLink').inner_text, 
     'description' => i.xpath('text/desc').inner_text,
     'price' => i.xpath('price/buynow').inner_text
   }
 end
end

在Rails的控制台中,我运行Products.import_from_feed(myfeedgoeshere)并得到:

[{"price"=>"8.00", "name"=>"BASIC GIRL BOXER", "description"=>"Boxer shorts Elasticated waist with Bench logo Button fly", "link"=>"http://www.bench.co.uk/womenswear/underwear/basic-girl-boxer/GY001X/"}, {"price"=>"10.00", "name"=>"CMTL  PK SPORTY SOCKS", "description"=>"Ankle sockBench logo on sole of each sockContrasting stripe around ankle", "link"=>"http://www.bench.co.uk/womenswear/underwear/cmtl03593-3-pk-sporty-socks/BK014-SK034/"}, {"price"=>"12.00", "name"=>"A PK STRING UNDERWEAR", "description"=>"Plain thong Bench logo along waistband Bench tag on front", "link"=>"http://www.bench.co.uk/womenswear/underwear/a4771-3pk-string-underwear/PK023-BK001-WH001/"}, {"price"=>"8.00", "name"=>"BASIC GIRL BOXER", "description"=>"Boxer shorts Elasticated waist with Bench logo Button fly", "link"=>"http://www.bench.co.uk/womenswear/underwear/basic-girl-boxer/WH001/"}, {"price"=>"45.00", "name"=>"OSPREY TRAINER", "description"=>"Lace up trainers Bench logo on tongue and back of heelBench logo on end of trainer", "link"=>"http://www.bench.co.uk/menswear/footwear/osprey-trainer/WH001-BL081/"}, {"price"=>"45.00", "name"=>"OSPREY TRAINER", "description"=>"Lace up trainers Bench logo on tongue and back of heelBench logo on said of trainer", "link"=>"http://www.bench.co.uk/menswear/footwear/osprey-trainer/WH001-GR128/"}, {"price"=>"90.00", "name"=>"META TRENCH", "description"=>"Vintage look leather bootLace upFabric sidesPull on tab on heel", "link"=>"http://www.bench.co.uk/womenswear/footwear/meta-trench/BK001/"}]
(^ Truncated)

有人可以告诉我如何访问数组的元素吗?循环播放,我可以获得@products.price@products.description等?

编辑:我已经尝试@products[0]products[0],我尝试过打印键/值对而没有任何运气。

我不是要求你做所有的工作,我认为这里有一些概念在起作用 - 足以让我碰到砖墙。

第2部分:额外信用!

根据选定的答案,这应该有效,对吗?

 @products.each do |h|
   h.save
 end

我明白了:

NoMethodError: undefined method `save' for #<Hash:0x10388c7d8>

3 个答案:

答案 0 :(得分:4)

由于@products的每个元素都是一个哈希,你可以这样做:

@products.each do |h|
  puts "#{h['price']}, #{h['description']}"
end

答案 1 :(得分:2)

没有测试过,但看起来你有一个哈希数组,所以首先循环遍历数组:

@products.each do |product_hash|

然后,对于每个项目(并且每个项目都是哈希),获取所需的元素: product_hash[:price]product_hash.price

@products.each do |product_hash|
#Do something with the price
product_hash[:price]
#Do other things...
end

答案 2 :(得分:1)

如果您想将这些值保存在数据库表中,例如Product,那么只需执行以下操作:

@products.each do |product|
    prod = Product.new
    prod.name = product["name"]
    prod.price = product["price"]
    prod.description = product["description"] 
    prod.save
end

您收到有关save关键字的错误,因为您没有执行任何数据库操作。