From: Chris White Date: 2011-09-14T00:31:14+09:00 Subject: Re: Using a loop to run similar commands > Is there a neater way to do this, by using a loop? > > create1 = agent.get(t1) > create2 = agent.get(t2) > create3 = agent.get(t3) > create4 = agent.get(t4) > create5 = agent.get(t5) > create6 = agent.get(t6) > create7 = agent.get(t7) > create8 = agent.get(t8) > create9 = agent.get(t9) > create10 = agent.get(t10) > create11 = agent.get(t11) > create12 = agent.get(t12) There's a lot of variable name repetition going on here. Instead I'd recommend using an array: t = [val1, val2...] Then you can store the end results in an array as well. Here is a more simplified example showing the basis of what you're trying to do: irb(main):006:0> test = [1,2,3,4] => [1, 2, 3, 4] irb(main):007:0> create = test.each_with_object([]) { |element, holder| holder << element.to_s } => ["1", "2", "3", "4"] In this case, you have an array with all the objects you want to run agent.get on. Then you loop through them using each_with_object, which passes in an array holder which has the results of calling a method on each element (in this case to_s). The result of this call will be a new array with the results of to_s called on each element. Regards, Chris White http://www.twitter.com/cwgem