Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Initializing a Struct from an array

Reply
Thread Tools

Initializing a Struct from an array

 
 
John Lam
Guest
Posts: n/a
 
      10-27-2005
------=_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--


 
Reply With Quote
 
 
 
 
Jamey Cribbs
Guest
Posts: n/a
 
      10-27-2005
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)




 
Reply With Quote
 
 
 
 
Brian Schröder
Guest
Posts: n/a
 
      10-27-2005
On 27/10/05, John Lam <(E-Mail Removed)> 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/


 
Reply With Quote
 
John Lam
Guest
Posts: n/a
 
      10-27-2005
------=_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--


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-28-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
How to access an Array and a struct within a struct wrapped by Swig ? emmanuel.rivoire@gmail.com Python 3 08-18-2008 03:30 AM
How to define an array of struct nested within a struct? Daniel Rudy C Programming 7 03-31-2006 06:54 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments