From: Morton Goldberg Date: 2007-12-12T03:47:26+09:00 Subject: Re: Creating multiple objects in loops On Dec 11, 2007, at 1:12 PM, Richard Harris wrote: > I was wondering if it is possible to create multiple objects within > loops, for example; > > 4.times do > #first loop: a = Object.new > #second loop: b = Object.new etc > end > > Also can you use the value of an array or hash as the name for an > object? e.g. > #array[1] has the value "a", create a new object with the name "a" > #e.g. a = Object.new > Whatever I try doesn't seem to work so I don't know if there is any > way > of doing it. Of course it's possible to create multiple objects in a loop. What you can't do is create variables inside a loop in the manner you wish. I think a hash will give you the behavior that is closest to what you want. For example: h = {} [:a, :b, :c, :d].each { |key| h[key] = Object.new } h[:a] # => # h[:b] # => # h[:c] # => # h[:d] # => # Regards, Morton