Creating multiple objects in loops

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.

Many thanks for any help

Richard

Richard H. wrote:

#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.

Yes, the problem you pose is very well solvable (see below), but …
what exactly are you trying to accomplish by dynamically defining named
local variables?

A wise Rubyist once said “eval is evil”, so beware!

array = %w[a b c]
=> [“a”, “b”, “c”]

array.map {|x| “#{x} = Object.new” }
=> [“a = Object.new”, “b = Object.new”, “c = Object.new”]

array.map {|x| “#{x} = Object.new” }.join(’; ')
=> “a = Object.new; b = Object.new; c = Object.new”

eval array.map {|x| “#{x} = Object.new” }.join(’; ')
=> #Object:0xb7d74fbc

a
=> #Object:0xb7d74fe4

b
=> #Object:0xb7d74fd0

c
=> #Object:0xb7d74fbc

Thanks for that but having looked at your solution, it may not be the
best idea. This is what I am trying to acomplish:

Text File

ID: 1, entry1 # Can have multiple ID’s
Name: Richard
Age: 20
Address: 123 Some Road # Can have multiple lines of address, not
fixed
Address: Some City
Address: Some Country
ID: 2
Name: Richard
Age: 20
Address: 123 Some Road
Address: Some City
Address: Some Country
ID: 3
Name: Richard
Age: 20
Address: 123 Some Road
Address: Some City
Address: Some Country

Parse the file and store the information so it can be printed out when
requested. So the info associated with ID: 1 is stored
together, info associated with ID: 2 is stored together etc. Then I will
have an array/hash of id’s. The info associated with
these id’s need to be printed, but I might only need id 1 and 2 so I
will have to search the stored info for the required id’s.

I have tried numerous ideas for this but some problem always arises
which stops me in my tracks. I have tried using hashes, objects etc but
as my knowledge of Ruby is limited I don’t know all the tricks so it
might be very simple.

Many thanks for any advice and if you can point me in the right
direction that would be great.

Cheers
R

2007/12/12, Richard H. [email protected]:

Address: Some City
Address: 123 Some Road

I have tried numerous ideas for this but some problem always arises
which stops me in my tracks. I have tried using hashes, objects etc but
as my knowledge of Ruby is limited I don’t know all the tricks so it
might be very simple.

Many thanks for any advice and if you can point me in the right
direction that would be great.

Are ids always showing up in order without holes? If so, you can
simply store them in arrays

Entry = Struct.new :name, :age, :addresses do
def initialize() self.addresses = [] end
end

data = []

io.each do |line|
line.chomp!
case line
when /^ID/
data << Entry.new
when /^Name:\s*(\S+)/
data.last.name = $1

end
end

Cheers

robert

On Dec 11, 2007, at 1:12 PM, Richard H. wrote:

#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

Maybe it’ll help if you tell us what you’re trying to do…
if you just want an array full of unique objects of some type,
something like this may be what you want.

whatever = Array.new(10){|i| i=Peas.new() }

–Kyle

PS
#yes, it’s tempting to skim the Array docs, and write
whatever = Array.new(10,Peas.new()
#BUT that would be an array of length ten, each element pointing to
the same pea,
#Not really what you want :slight_smile:

On 12/11/07, Suraj K. [email protected] wrote:

eval array.map {|x| “#{x} = Object.new” }.join(’; ')
=> #Object:0xb7d74fbc

a
=> #Object:0xb7d74fe4

b
=> #Object:0xb7d74fd0

c
=> #Object:0xb7d74fbc

unfortunately, this only works in IRB, not in a script:

C:\code>type dynamicVarNames.rb
array = %w[a b c]
p array.map {|x| “#{x} = Object.new” }.join(’; ‘)
eval array.map {|x| “#{x} = Object.new” }.join(’; ')
p a,b,c

C:\code>ruby dynamicVarNames.rb
“a = Object.new; b = Object.new; c = Object.new”
dynamicVarNames.rb:5: undefined local variable or method `a’ for
main:Object (NameError)

I’m not sure why, exactly. something about variable scope. Maybe
some guru can explain. It will work if you set array = %w{@a @b @c},
but then you are creating instance variables, not locals= ones.

-Adam

Basically it’s what I mentioned in my second post (3rd one down).
Reading in a text file, parsing it and storing it so I can retrieve it
later. I have sorted the reading in and parsing, it’s just the best way
of storing the information.

Text File

ID: 1, entry1 # Can have multiple ID’s
Name: Richard
Age: 20
Address: 123 Some Road # Can have multiple lines of address, not
fixed
Address: Some City
Address: Some Country
ID: 2
Name: Richard
Age: 20
Address: 123 Some Road
Address: Some City
Address: Some Country
ID: 3
Name: Richard
Age: 20
Address: 123 Some Road
Address: Some City
Address: Some Country

The text file is in this format but can have multiple id’s, and multiple
addresses. Also it can have a different amount of fields so one entry
might not have an age for example. When it reads in the file it has to
then store the entries in a hash for example so all the info below the
ID: bit is stored together.

e.g. contacts{{“ID”=>{“1”,“entry1”},“Name”=>“Richard”,“Address”=>{“123
Some Road”,“some City”,“Some
Country”}},{“ID”=>{“2”},“Name”=>“Richard”,“Address”=>{“123 Some
Road”,“some City”,“Some Country”}}}

or something similar, along those lines. So I can say I want the address
from the contacts hash where ID equals 1.

The thing is I have tried this and can’t seem to get it to work at all.
any thoughts on how to go about it or another way.

Cheers for this

R

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs