Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4692 > unrolled thread

need help with "module" and variable

Started bykeinlezard <keinlezard@yahoo.fr>
First post2011-05-17 22:35 +0200
Last post2011-05-20 18:06 -0500
Articles 6 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  need help with "module" and variable keinlezard <keinlezard@yahoo.fr> - 2011-05-17 22:35 +0200
    Re: need help with "module" and variable 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-17 16:57 -0500
      Re: need help with "module" and variable keinlezard <keinlezard@yahoo.fr> - 2011-05-20 21:43 +0200
        Re: need help with "module" and variable 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-20 15:35 -0500
          Re: need help with "module" and variable keinlezard <keinlezard@yahoo.fr> - 2011-05-21 15:41 +0200
    Re: need help with "module" and variable Gary Wright <gwtmp01@mac.com> - 2011-05-20 18:06 -0500

#4692 — need help with "module" and variable

Fromkeinlezard <keinlezard@yahoo.fr>
Date2011-05-17 22:35 +0200
Subjectneed help with "module" and variable
Message-ID<4dd2dc25$0$27524$426a74cc@news.free.fr>
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

[toc] | [next] | [standalone]


#4699

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-17 16:57 -0500
Message-ID<93d3fa9ddf7eb83eb6bb5ab4ce7082c6@ruby-forum.com>
In reply to#4692
Don't use global variables--ever.

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

[toc] | [prev] | [next] | [standalone]


#4835

Fromkeinlezard <keinlezard@yahoo.fr>
Date2011-05-20 21:43 +0200
Message-ID<4dd6c46c$0$11353$426a74cc@news.free.fr>
In reply to#4699
Le 17/05/2011 23:57, 7stud -- a écrit :
> 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 ...

[toc] | [prev] | [next] | [standalone]


#4838

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-20 15:35 -0500
Message-ID<46673c8478a5e2515cc7bee692047a21@ruby-forum.com>
In reply to#4835
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/.

[toc] | [prev] | [next] | [standalone]


#4859

Fromkeinlezard <keinlezard@yahoo.fr>
Date2011-05-21 15:41 +0200
Message-ID<4dd7c0ff$0$690$426a74cc@news.free.fr>
In reply to#4838
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.
>


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

greetings

[toc] | [prev] | [next] | [standalone]


#4842

FromGary Wright <gwtmp01@mac.com>
Date2011-05-20 18:06 -0500
Message-ID<D58B7EC7-9A58-4B41-8305-B87EA8C4C818@mac.com>
In reply to#4692
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


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


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web