Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access hashes within an array?

I'm trying to use Nokogiri to grab some data from an XML file, then save it into the database.

The code I'm using is:

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

In Rails' console, I ran Products.import_from_feed(myfeedgoeshere) and got:

[{"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)

Can someone tell me how I can access elements of the array? Loop through so I can get @products.price, @products.description etc?

Edit: I've tried @products[0], products[0], I've tried printing key/value pairs without any luck.

I'm not asking you to do all the work, I think it's that there are a few concepts at work here - enough to keep me hitting brick walls.

Part 2: Extra Credit!

Based on the selected answer, this should work, right?

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

I get:

NoMethodError: undefined method `save' for #<Hash:0x10388c7d8>
like image 594
Geoff Wright Avatar asked Apr 26 '11 20:04

Geoff Wright


1 Answers

Since each element of @products is a hash, you could do this:

@products.each do |h|
  puts "#{h['price']}, #{h['description']}"
end
like image 99
buruzaemon Avatar answered Sep 22 '22 05:09

buruzaemon