Initializing a Struct from an array

Discussion in 'Ruby' started by John Lam, Oct 27, 2005.

  1. John Lam

    John Lam Guest

    ------=_Part_4331_20153101.1130431923854
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    I'm writing some code to dump database tables into in-memory hashtables.
    Here's an example:

    Foo =3D Struct.new('Foo', :id, :a, :b, :c)
    conn.select_all('select * from foo').each do |h|
    current =3D Foo.new(h[0], h[1], h[2], h[3])
    @foo_map[current.id <http://current.id>] =3D current
    end

    It's the Foo.new ... part that bugs me. Is there a nice clean way of
    initializing a Struct from an array?

    Thanks
    -John
    http://www.iunknown.com

    ------=_Part_4331_20153101.1130431923854--
     
    John Lam, Oct 27, 2005
    #1
    1. Advertising

  2. John Lam

    Jamey Cribbs Guest

    John Lam wrote:

    >I'm writing some code to dump database tables into in-memory hashtables.
    >Here's an example:
    >
    >Foo = Struct.new('Foo', :id, :a, :b, :c)
    >conn.select_all('select * from foo').each do |h|
    >current = Foo.new(h[0], h[1], h[2], h[3])
    >@foo_map[current.id <http://current.id>] = current
    >end
    >
    >It's the Foo.new ... part that bugs me. Is there a nice clean way of
    >initializing a Struct from an array?
    >
    >

    current = Foo.new(*h)
     
    Jamey Cribbs, Oct 27, 2005
    #2
    1. Advertising

  3. On 27/10/05, John Lam <> wrote:
    > I'm writing some code to dump database tables into in-memory hashtables.
    > Here's an example:
    >
    > Foo =3D Struct.new('Foo', :id, :a, :b, :c)
    > conn.select_all('select * from foo').each do |h|
    > current =3D Foo.new(h[0], h[1], h[2], h[3])
    > @foo_map[current.id <http://current.id>] =3D current
    > end
    >
    > It's the Foo.new ... part that bugs me. Is there a nice clean way of
    > initializing a Struct from an array?
    >
    > Thanks
    > -John
    > http://www.iunknown.com
    >
    >


    Foo.new(*h)

    generally

    irb(main):002:0* def t(a, b =3D nil, c =3D nil)
    irb(main):003:1> p [a,b,c]
    irb(main):004:1> end
    =3D> nil
    irb(main):005:0>
    irb(main):006:0* t(1)
    [1, nil, nil]
    =3D> nil
    irb(main):007:0> t(1,2,3)
    [1, 2, 3]
    =3D> nil
    irb(main):008:0> t([1,2,3])
    [[1, 2, 3], nil, nil]
    =3D> nil
    irb(main):009:0> t(*[1,2,3])
    [1, 2, 3]
    =3D> nil

    hth,

    brian

    --
    http://ruby.brian-schroeder.de/

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
     
    Brian Schröder, Oct 27, 2005
    #3
  4. John Lam

    John Lam Guest

    ------=_Part_4583_8957989.1130432393006
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Thanks, guys!

    ------=_Part_4583_8957989.1130432393006--
     
    John Lam, Oct 27, 2005
    #4
  5. John Lam wrote:
    > Thanks, guys!


    Additional recommendation: don't do "select *" in production code, always
    explicitely query those columns you need. Why? If there are changes your
    code will still work but since the order and number of fields read by
    "select *" is not fixed you risk later errors that even may go undetected
    for a while.

    Kind regards

    robert
     
    Robert Klemme, Oct 28, 2005
    #5
    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. mark fine
    Replies:
    1
    Views:
    479
    Michael Jørgensen
    Nov 9, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,433
    Chris Fogelklou
    Apr 20, 2004
  3. Carramba

    initializing struct

    Carramba, Jun 11, 2007, in forum: C Programming
    Replies:
    13
    Views:
    631
    Barry Schwarz
    Jun 12, 2007
  4. Tuan  Bui
    Replies:
    14
    Views:
    507
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  5. Noob
    Replies:
    59
    Views:
    776
    Tim Rentsch
    Jan 24, 2013
Loading...

Share This Page