On Mon, 5 Mar 2007, Bertram Scharpf wrote:

> Hi,
>
> Am Montag, 26. Feb 2007, 18:05:38 +0900 schrieb jef peeraer:
>> i am a real postgresql addict so i installed postgres gem ( which is
>> version 0.7.1, i think). Everything works like a charm, but there is
>> some basic functionality i miss : i want to return tupples in a hash
>> instead of an array. As an former PHP user, this was a basic thing to
>> do. I googled a bit, and found some posts about a new version ( 0.8 ),
>> which could handle these hashes. But the actual gem is 0.7.1 .
>> What can i do ?
>
> Write your own patch. I really mean it; it's not so
> difficult.
>
> I don't actually understand what the feature is you are
> missing. Could you describe it more precisely? Maybe one of
> my patches is appropriate.
>
> Bertram

arrayfields makes this very easy:


fortytwo :~ > cat a.rb
require 'rubygems'
require 'postgres'
require 'arrayfields'

pgconn = PGconn.new

pgconn.exec <<-sql
   drop table t
sql

pgconn.exec <<-sql
   create table t(
     x text,
     y text
   )
sql

pgconn.exec <<-sql
   insert into t values ('foo', 'bar');
   insert into t values ('a', 'b');
sql

result = pgconn.exec <<-sql
   select * from t;
sql

result.each do |row|
   row.fields = result.fields
   p row.values_at('x', 'y')
   p row['x']
   p row['y']
end


fortyytwo :~ > ruby a.rb
["foo", "bar"]
"foo"
"bar"
["a", "b"]
"a"
"b"


in fact, it was for the postgres library that i coded it.

regards.

-a
-- 
be kind whenever possible... it is always possible.
- the dalai lama