From: Josh Cheek Date: 2011-09-14T22:52:56+09:00 Subject: Re: Using a loop to run similar commands --20cf3071cd3425548004ace63ff7 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Sep 14, 2011 at 7:22 AM, dwight schrute wrote: > I actually played around a bit and then did this: > > 1.upto(12) do > |i| eval "create#{i} = agent.get(t#{i})" > end > > -- > Posted via http://www.ruby-forum.com/. > > In the same way that you can store a variable named "create1" in your local binding, you can store it in a hash, or you can use indexes rather than suffixes and store it in an array. This is safer (you can't accidentally execute arbitrary code), faster (you don't have to parse source code), much more robust (arrays and hashes are used everywhere, but it would be a nightmare to pass bindings all over the place, evaling on them to get the values you need). Please check out my above example again, or check out hashes. In Ruby Kickstart, we talk about Arrays in session 2 http://ruby-kickstart.com/#session2 and Hashes in session 3 http://ruby-kickstart.com/#session3 We might go a bit quick for a beginner, I'm not sure, but seeing the examples might be helpful. --20cf3071cd3425548004ace63ff7--