From: Gary Wright Date: 2011-05-21T08:06:36+09:00 Subject: Re: need help with "module" and variable On May 17, 2011, at 4:40 PM, keinlezard wrote: >> module DB_parc >> Toto=2 >> def DB_parc::session() >> begin >> dbh = DBI.connect(CONF_parc::Db,CONF_parc::User,CONF_parc::Password); >> #backend = Backend.new(dbh) >> #yield(backend) >> # yield :provoque l'execution de la variable passée en parametre >> yield(dbh) >> rescue DBI::Error => e >> puts "#{e.errstr}" >> ensure >> dbh.disconnect if dbh >> end >> end >> end > > And in another file I have something like > >> DB_parc.session{|dbh| >> schema="sparc" > >> .... } > > I try to get the dbh handler because I need to write a function that use it ... may be I'm wrong and it exits another way to do this > > My funtion is something like that > > > def List(some parameters) > query_tab_mater="select id, label from sparc.type_materiel order by label asc" > sth=dbh.execute(query_tab_mater) > sth.fetch{|k,v| > output+="" > } > output+= " " > end make sure your definition of List includes a parameter for the dbh handle: def List(dbh, other, parameters) # stuff end Now use something like this: DB_parc.session { |dbh| schema="sparc" List(dbh, schema, other, stuff) } Gary Wright