Which data-type can I use for this task?

Discussion in 'Ruby' started by Adna rim, Feb 15, 2008.

  1. Adna rim

    Adna rim Guest

    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?
     
    Adna rim, Feb 15, 2008
    #1
    1. Advertising

  2. Adna rim

    Day Guest

    On Fri, Feb 15, 2008 at 10:34 AM, Adna rim <> 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
     
    Day, Feb 15, 2008
    #2
    1. Advertising

  3. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Siep Korteling, Feb 15, 2008
    #3
  4. On 15.02.2008 17:32, 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...


    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
     
    Robert Klemme, Feb 15, 2008
    #4
  5. Robert Klemme 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,: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
    --
    Posted via http://www.ruby-forum.com/.
     
    Siep Korteling, Feb 15, 2008
    #5
  6. Siep Korteling wrote:
    > 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:
    >> c =3D Class.new

    =3D> #<Class:0x2acfa00d27e0>
    (anonymous class)
    >> FooBar =3D c

    =3D> FooBar
    >> c

    =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:
    ICQ: 205544826
     
    Sebastian Hungerecker, Feb 16, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor
    Replies:
    0
    Views:
    8,906
    Victor
    Sep 1, 2004
  2. krabhi
    Replies:
    1
    Views:
    9,458
    Marco Meschieri
    Aug 9, 2006
  3. teggy
    Replies:
    0
    Views:
    849
    teggy
    May 29, 2007
  4. Mike
    Replies:
    1
    Views:
    1,985
    GArlington
    May 12, 2008
  5. Stéphane Wirtel
    Replies:
    3
    Views:
    414
    Stephane Wirtel
    Jun 15, 2007
Loading...

Share This Page