Need help with "module" and variable

hello

  My problem is

   I have this code in a file

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+="<option value='#{v}'"
                          type_materiel = 'non-connu'
                          if v == type_materiel
                                  output+= " selected='selected'"
                          end
                          output+= ">#{v}</option>"
                      }
           output+= "</select></td></tr> "
end

but at excecution time ruby complain about dbh not known

can somebody help me and explain it

thank you

Don't use global variables--ever.

···

--
Posted via http://www.ruby-forum.com/.

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

···

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+="<option value='#{v}'"
                       type_materiel = 'non-connu'
                       if v == type_materiel
                               output+= " selected='selected'"
                       end
                       output+= ">#{v}</option>"
                   }
        output+= "</select></td></tr> "
end

From what I've seen, whenever someone uses a global variable, it's
because the person washaving difficulty figuring out how to get data from
one scope into another (and sometimes back again). It's true that using
global variables is pretty much never the right answer, if for no other
reason than the fact it can cause issues if there are two globals with
the same name.

So, yeah, yours is decent advice. There's just one problem: when people
say "don't use globals" and leave it at that, they give the people who
used globals no ideas what other recourse they have available to them.
Don't just say "don't use global variables". Try saying "You could do
this other thing instead, to get what you need without using a global
variable."

···

On Wed, May 18, 2011 at 06:57:59AM +0900, 7stud -- wrote:

Don't use global variables--ever.

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

Don't use global variables--ever.

I understand that ... what I want to know...

Is how make that without this damned global variables ....

It seems to me that my code do not use global variable ...

I meet this kid of problem when I try to write

                        query_tab_propriet="select id, label from sparc.proprietaire order by label asc"
                        sth=dbh.execute(query_tab_propriet)
def funct(sth)
        sth.fetch{|k,v|
           output="<option value='#{v}'"
           label_proprietaire = 'non-defini'
           if v == label_proprietaire
                        output+= " selected='selected'"
           end
          output+= ">#{v}</option>"
        }
end

output+=funct(sth)

to replace

                        query_tab_propriet="select id, label from sparc.proprietaire order by label asc"
                        sth=dbh.execute(query_tab_propriet)
        sth.fetch{|k,v|
           output+="<option value='#{v}'"
           label_proprietaire = 'non-defini'
           if v == label_proprietaire
                        output+= " selected='selected'"
           end
          output+= ">#{v}</option>"
        }

I know that an horrible code ... in fact that is just another test ...
that produce same problem than my other chunk of code ...

···

Le 17/05/2011 23:57, 7stud -- a écrit :

keinlezard wrote in post #999948:

···

Le 17/05/2011 23:57, 7stud -- a écrit :

Don't use global variables--ever.

I understand that ... what I want to know...

You posted no code in your first post, and you posted no code in your
next post. So no one has any idea what your questions is or what you are
talking about.

--
Posted via http://www.ruby-forum.com/\.

keinlezard wrote in post #999948:
>> Don't use global variables--ever.
>>
>
> I understand that ... what I want to know...

You posted no code in your first post, and you posted no code in your
next post. So no one has any idea what your questions is or what you are
talking about.

Is there something wrong with your mail user agent? I saw code. This is
an excerpt from the original message:

      I have this code in a file

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

I'm not sure why he has blockquote characters at the beginnings of all
those lines of code, but that's definitely code -- and it's not all the
code that was in that message, either.

···

On Sat, May 21, 2011 at 05:35:24AM +0900, 7stud -- wrote:

> Le 17/05/2011 23:57, 7stud -- a écrit :

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

That is curious .. because ... I post with code ...
Problem with your provider / News reader / or something else ... I suppose ...

greetings

···

Le 20/05/2011 22:35, 7stud -- a écrit :

keinlezard wrote in post #999948:

Le 17/05/2011 23:57, 7stud -- a écrit :

Don't use global variables--ever.

I understand that ... what I want to know...

You posted no code in your first post, and you posted no code in your
next post. So no one has any idea what your questions is or what you are
talking about.