From: Matthew Kerwin Date: 2012-05-25T16:22:34+09:00 Subject: Re: accessing data in a ruby array The keys in the hash appear to be strings, so use: product[1]["id"] Although what you wrote doesn't match the data structure, so I'm kind of guessing here. On 25 May 2012 17:11, Maksim D. wrote: > I have this array. I want to access the stuff in the hashes. > > i can loop through them all and get the individual hashes but i cannot > access the data. > > products.each.do |product| >     product[1] >     #this gives me the hash >     #product[1].id and product[1][:id] does not work > end > > How do i access the name and description and rateplans inside this > structure. > > Thanks in advance! > > products => ["prod0", {"id"=>"4028e6963457a2a001345934b93633e4", > "name"=>"Product 1", "description"=>"This is a cool product with 4 rate > plans of different frequencies", > "rate_plans"=>{"rp0"=>{"id"=>"4028e6963457a2a001345936b60d33fa", > "name"=>"Quarterly Rate Plan", "description"=>"This is a Quarterly Rate > Plan billed every quarter.", "uom"=>"Seat", > "charges"=>{"rpc0"=>{"id"=>"4028e697343fffc20134593a0a242ebd", > "uom"=>"Seat", "quantity"=>"5", "name"=>"New Component", "price"=>0, > "charge_model"=>"Per Unit Pricing"}}, "quantifiable"=>true}, > "rp1"=>{"id"=>"4028e697343fffc2013459355d232e61", "name"=>"Monthly Rate > Plan", "description"=>"This is the Monthly Rate Plan which is the most > basic plan", "uom"=>"Seat", > "charges"=>{"rpc0"=>{"id"=>"4028e697343fffc20134593678b12e65", > "uom"=>"Seat", "quantity"=>"5", "name"=>"New Component", "price"=>0, > "charge_model"=>"Per Unit Pricing"}}, "quantifiable"=>true}, > "rp2"=>{"id"=>"4028e697343fffc20134593a4b652ec4", "name"=>"Semi-Annual > Rate Plan", "description"=>"This is a Semi-Annual Rate Plan billed every > 6 months.", "uom"=>"Seat", > "charges"=>{"rpc0"=>{"id"=>"4028e6963457a2a00134593ac09f3415", > "uom"=>"Seat", "quantity"=>"5", "name"=>"New Component", "price"=>0, > "charge_model"=>"Per Unit Pricing"}}, "quantifiable"=>true}, > "rp3"=>{"id"=>"4028e697343fffc20134593b63272ecd", "name"=>"Annual Rate > Plan", "description"=>"This is an Annual Rate Plan billed yearly", > "uom"=>"Each", > "charges"=>{"rpc0"=>{"id"=>"4028e6963457a2a00134593ba494343a", > "uom"=>"Each", "quantity"=>"5", "name"=>"New Component", "price"=>0, > "charge_model"=>"Per Unit Pricing"}}, "quantifiable"=>true}}}] > > -- > Posted via http://www.ruby-forum.com/. > --   Matthew Kerwin, B.Sc (CompSci) (Hons)   http://matthew.kerwin.net.au/   ABN: 59-013-727-651   "You'll never find a programming language that frees   you from the burden of clarifying your ideas." - xkcd