Which data-type can I use for this task?

Hi,
I have a little question. I have a list here like this:

street
house
person
age

street
house
person
age

street
house
person
age

Now I wanna store them in a data type like an array. I tied this here:

information = [Street=[],House=[],Person=[],Age=[]]

but If I wanna now fill it like

information.Street << firstentry

I always get an error telling undefined method Street… With which
datatype do I best realize this?

On Fri, Feb 15, 2008 at 10:34 AM, Adna rim [email protected] wrote:

Now I wanna store them in a data type like an array. I tied this here:

    information = [Street=[],House=[],Person=[],Age=[]]

but If I wanna now fill it like

    information.Street << firstentry

I always get an error telling undefined method Street… With which
datatype do I best realize this?

I vote for an array of hashes. Each entry is a hash, composed thusly:
info = {:street => ‘1234 Street st.’, :house => ‘purple’, :person =>
‘some guy’, :age => ‘a billion.’}

So you can set a given piece with:
info[:street] = ‘4321 Avenue Blvd.’

Sound like what you want?

Ben

Adna rim wrote:

Hi,
I have a little question. I have a list here like this:

street
house
person
age

street
house
person
age

street
house
person
age

Now I wanna store them in a data type like an array. I tied this here:

information = [Street=[],House=[],Person=[],Age=[]]

but If I wanna now fill it like

information.Street << firstentry

I always get an error telling undefined method Street… With which
datatype do I best realize this?

I prefer a Struct is for this.

row=Struct.new(:street,:house)
d=[]
d<<row.new(“Baker street”, “14b”)
d<<row.new(“Sesame street”,“nope”) # this should be in a loop
puts d[1].house

regards,

Siep

Robert K. wrote:

(…)(probably using a constant for the Struct and not a local
variable because Struct.new really defines a new class).

Cheers

robert

I didn’t know this. I never noticed it, but all examples I used to learn
from do indeed use Constants.

row=Struct.new(:street,:house)
d=[]
d<<row.new(“Baker street”, “14b”)
d<<row.new(“Sesame street”,“nope”) # this should be in a loop
puts d[0].house

So changing “row” to “Row” is better?

a=Struct.new(:s,:o)
A=Struct.new(:s,:o)
puts a.new(1,2).class
puts A.new(1,2).class

=>#Class:0x2c27534
A

Hmm. “A” is a full blown Class (as Robert wrote), and “a” is reference
to an anonimous Class, provided by Ruby to aid clumsy programmers?
I have probably some cleaning up to do tomorrow.

Thank you,

Siep

Siep K. wrote:

Hmm. “A” is a full blown Class (as Robert wrote), and  “a” is reference
to an anonimous Class, provided by Ruby to aid clumsy programmers?

An anonymous class is a full blown class, too. The only difference
between a
named class and an anonymous class is that the named class has been
assigned
to a constant which then became its name. Example:

c = Class.new
=> #Class:0x2acfa00d27e0
(anonymous class)

FooBar = c
=> FooBar

c
=> FooBar
It’s still the same class, but its name is now FooBar because that’s the
constant it has been assigned to.

HTH,
Sebastian

On 15.02.2008 17:32, Adna rim wrote:

person
information = [Street=[],House=[],Person=[],Age=[]]

but If I wanna now fill it like

information.Street << firstentry

I always get an error telling undefined method Street…

Yes, because you assigned to the global constant “Street” and did not
define a method with that name.

With which datatype do I best realize this?

See Siep’s suggestion. Of course it depends on how you want to work
with the data but the most likely case is that you treat one set of
street, house etc. as a single record so it makes sense to do it the way
Siep suggested (probably using a constant for the Struct and not a local
variable because Struct.new really defines a new class).

Cheers

robert