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 <<agent
alias_method :get, :fetch
end
# instead of local variables t1, t2, ..., tn
# use an array named t, and put the values in there
# at the appropriate indexes
t = ['t0', 't1', 't2', 't3']
# instead of local variables create1, create2, ..., createn
# use an array named create, and put the values in there
create =
# for each of the values of t, get them from agent
# and put them into create
t.each do |t_i|
create << agent.get(t_i)
end
# and now the create hash holds all the values that
# the local variables would have had to hold
create # => ["value for t0", "value for t1", "value for t2", "value for t3"]
···
On Tue, Sep 13, 2011 at 10:14 AM, dwight schrute <spambocks@yahoo.ca> 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/\.