From: Josh Cheek Date: 2011-09-14T00:43:05+09:00 Subject: Re: Using a loop to run similar commands --f46d042fdb3edf21b504acd46361 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Sep 13, 2011 at 10:14 AM, dwight schrute wrote: > Hi again, > > 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) > > Thanks guys, > > Dwight > > -- > Posted via http://www.ruby-forum.com/. > > Yes, but it's flagrantly wrong. You're creating consecutive variable names, thus using the current binding as if it were a hash or an array (depending on how literally you want to interpret it). What you should be doing is using an actual hash or array. Here is an example: # agent will be a hash that represents # whatever your agent is in your use case agent = { 't0' => 'value for t0', 't1' => 'value for t1', 't2' => 'value for t2', 't3' => 'value for t3', } # give it a get method so it looks like your agent class < ["value for t0", "value for t1", "value for t2", "value for t3"] --f46d042fdb3edf21b504acd46361--