From: Tony Arcieri Date: 2012-01-04T10:31:48+09:00 Subject: Re: Looping through an array of hashes? help --0015174c1e3a656ccb04b5a9c785 Content-Type: text/plain; charset=ISO-8859-1 Err whoops, here's a version without the Water::IE duck type class I made :) https://gist.github.com/1557924 On Tue, Jan 3, 2012 at 5:30 PM, Tony Arcieri wrote: > On Tue, Jan 3, 2012 at 4:00 PM, Shawn M. wrote: > >> I'm new any ideas? >> >> require "rubygems" >> require "watir" >> >> x = 0 >> createDailyHash() >> $ie = Watir::IE.new >> >> $testDailies.each{ |i| >> $ie.goto $testDailies[x]["Link"] >> x = x + 1 >> } >> > > The each { |i| there doesn't give you an index like you're expecting. > Instead, it gives you an object. Because of this, you don't need an i or an > x there. You just need a variable to receive the hash. > > I cleaned your example up into a bit more idiomatic Ruby: > > https://gist.github.com/1557924/0145399c2346044779cfa9bd9b4fe08ec00aac79 > > Just for fun, here's a version that stores the array of data you're > populating at the end of the script as a YAML file ;) > > https://gist.github.com/1557924/56b61f69e5600f0f080ab775e62cac45151fd188 > > > >> >> >> >> >> def createDailyHash() >> >> $testDailies = Array.new >> >> $testDailies[0] = {"Name" => "jelly", "Link" => >> "http://www.google.com", "ID" => "0"} >> $testDailies[1] = {"Name" => "omelette", "Link" => >> "http://www.google.com", "ID" => "1"}, >> $testDailies[2] = {"Name" => "giveaway", "Link" => >> "http://www.google.com", "ID" => "2"}, >> $testDailies[3] = {"Name" => "bankInterest", "Link" => >> "http://www.google.com", "ID" => "3"}, >> $testDailies[4] = {"Name" => "shopOfOffers", "Link" => >> "http://www.google.com", "ID" => "4"}, >> $testDailies[5] = {"Name" => "petPark", "Link" => >> "http://www.google.com", "ID" => "5"}, >> $testDailies[6] = {"Name" => "freebies", "Link" => >> "http://www.google.com", "ID" => "6"}, >> $testDailies[7] = {"Name" => "obsidian", "Link" => >> "http://www.google.com", "ID" => "7"}, >> $testDailies[8] = {"Name" => "appleBobbing", "Link" => >> "http://www.google.com", "ID" => "8"}, >> $testDailies[9] = {"Name" => "anchor", "Link" => >> "http://www.google.com", "ID" => "9"}, >> $testDailies[10] = {"Name" => "advert", "Link" => >> "http://www.google.com", "ID" => "10"}, >> $testDailies[11] = {"Name" => "tombola", "Link" => >> "http://www.google.com", "ID" => "11"}, >> $testDailies[12] = {"Name" => "nowager", "Link" => >> "http://www.google.com", "ID" => "12"}, >> $testDailies[13] = {"Name" => "fruitMachine", "Link" => >> "http://www.google.com", "ID" => "13"}, >> $testDailies[14] = {"Name" => "coltazan", "Link" => >> "http://www.google.com", "ID" => "14"}, >> $testDailies[15] = {"Name" => "tdmbgpop", "Link" => >> "http://www.google.com", "ID" => "15" >> end >> >> Attachments: >> http://www.ruby-forum.com/attachment/6883/testHashLoop.rb >> >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> > > > -- > Tony Arcieri > > -- Tony Arcieri --0015174c1e3a656ccb04b5a9c785--