# Which data-type can I use for this task?

**URL:** <https://www.ruby-forum.com/t/which-data-type-can-i-use-for-this-task/129235>\
**Category:** Ruby\
**Created:** [February 15, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/which-data-type-can-i-use-for-this-task/129235 "2008-02-15T17:36:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adna\_rim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@Adna\_rim](https://www.ruby-forum.com/u/Adna_rim)\
**Post date:** [February 15, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/which-data-type-can-i-use-for-this-task/129235/1 "2008-02-15T17:36:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Adna\_rim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@Adna\_rim](https://www.ruby-forum.com/u/Adna_rim)\
**Post date:** [February 15, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/which-data-type-can-i-use-for-this-task/129235/2 "2008-02-15T17:47:57Z")

</div>

On Fri, Feb 15, 2008 at 10:34 AM, Adna rim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Adna\_rim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@Adna\_rim](https://www.ruby-forum.com/u/Adna_rim)\
**Post date:** [February 15, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/which-data-type-can-i-use-for-this-task/129235/3 "2008-02-15T18:13:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adna\_rim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@Adna\_rim](https://www.ruby-forum.com/u/Adna_rim)\
**Post date:** [February 16, 2008, 12:52am UTC](https://www.ruby-forum.com/t/which-data-type-can-i-use-for-this-task/129235/4 "2008-02-16T00:52:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adna\_rim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@Adna\_rim](https://www.ruby-forum.com/u/Adna_rim)\
**Post date:** [February 16, 2008, 12:17pm UTC](https://www.ruby-forum.com/t/which-data-type-can-i-use-for-this-task/129235/5 "2008-02-16T12:17:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Adna\_rim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@Adna\_rim](https://www.ruby-forum.com/u/Adna_rim)\
**Post date:** [February 15, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/which-data-type-can-i-use-for-this-task/129235/6 "2008-02-15T23:18:18Z")

</div>

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
