How to go through a loop and create a new object with each iteration Ruby? -


i saw several variations of question did not find solid answer.

so have array of urls. want loop through array , each individual url, create instance of class webpages.

so if array urls has 5 urls in it, create 5 objects of webpages. tried use eval() learned instances made eval have local scope , cannot use webpage objects after.

string_to_eval = @urls.map{|x| "webpage#{urls.index(x)} = webpage.new('#   {x}')"}.join(';') puts string_to_eval eval(string_to_eval) 

string_to_eval prints out:

webpage0 = webpage.new('http://www.google.com'); webpage1 = webpage.new('http://www.yahoo.com'); webpage2 = webpage.new('http://www.amazon.com'); webpage3 = webpage.new('http://www.ebay.com') 

how else can make object each iteration of loop in ruby? there way around this?

why not this?

webpages = @urls.map { |url| webpage.new(url) } 

it bad idea have webpage0, webpage1... when can have webpages[0], webpages[1]... (also, array way not require evil of eval.)