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