R
Raphael Bauduin
Hi,
I'm working woth the ruby postgresql interface. Due to my previous use
of other languages, I am used to have access to a retrieved row in the
form of a Hash( "field" => field_name, value => cell_value) .
Now I write this in ruby:
res.result.each do |tupl|
tupl.each do |fld|
puts "#{res.fields[tupl.index(fld)]} = #{fld}"
end
end
while I'm used to write something like this:
res.result.each_hash do |tupl|
tupl.each_pair do |fld, value|
puts "#{field} = #{value}
end
end
I'm not pretending the second solution is best... (hashes don't ensure
order)
It's just I found this functionality very handy in some cases and it
brought me to a question:
what's the easiest way to build that hash for a row? I have the keys in
res.fields, and the values in tupl. With that info, can I directly build
the hash, with a constructor like row_hash=Hash.new(res.fields, tupl) ?
Thanks.
Raph
I'm working woth the ruby postgresql interface. Due to my previous use
of other languages, I am used to have access to a retrieved row in the
form of a Hash( "field" => field_name, value => cell_value) .
Now I write this in ruby:
res.result.each do |tupl|
tupl.each do |fld|
puts "#{res.fields[tupl.index(fld)]} = #{fld}"
end
end
while I'm used to write something like this:
res.result.each_hash do |tupl|
tupl.each_pair do |fld, value|
puts "#{field} = #{value}
end
end
I'm not pretending the second solution is best... (hashes don't ensure
order)
It's just I found this functionality very handy in some cases and it
brought me to a question:
what's the easiest way to build that hash for a row? I have the keys in
res.fields, and the values in tupl. With that info, can I directly build
the hash, with a constructor like row_hash=Hash.new(res.fields, tupl) ?
Thanks.
Raph