Which data-type can I use for this task?

A

Adna rim

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?
 
D

Day

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
 
S

Siep Korteling

Adna said:
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
 
R

Robert Klemme

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

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
 
S

Siep Korteling

Robert said:
(...)(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,:eek:)
A=Struct.new:)s,:eek:)
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
 
S

Sebastian Hungerecker

Siep said:
Hmm. "A" is a full blown Class (as Robert wrote), and =C2=A0"a" is refere= nce
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=20
named class and an anonymous class is that the named class has been assigne=
d=20
to a constant which then became its name. Example:=3D> FooBar
It's still the same class, but its name is now FooBar because that's the=20
constant it has been assigned to.


HTH,
Sebastian
=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top