Which class variable is set here?

Here is part of a code snippet posted earlier.

require 'postgres’
module Apache
class RubyRun
@@pg_conn_handler = nil

    def RubyRun.pg_conn
        if !@@pg_conn_handler

.....

I believe that

@@pg_conn_handler = nil

is not set in the context of a method so there will be set an object
variable from … which class?

Greetings

···


Fritz Heinrichmeyer mailto:fritz.heinrichmeyer@fernuni-hagen.de
FernUniversitaet Hagen, LG ES, 58084 Hagen (Germany)
tel:+49 2331/987-1166 fax:987-355 http://www-es.fernuni-hagen.de/~jfh

“Fritz Heinrichmeyer” fritz.heinrichmeyer@fernuni-hagen.de wrote in
message news:86fzyic0nh.fsf@jfh00.fernuni-hagen.de

Here is part of a code snippet posted earlier.

require 'postgres’
module Apache
class RubyRun
@@pg_conn_handler = nil

    def RubyRun.pg_conn
        if !@@pg_conn_handler

.....

I believe that

@@pg_conn_handler = nil

is not set in the context of a method so there will be set an object
variable from … which class?

Greetings

Fritz Heinrichmeyer mailto:fritz.heinrichmeyer@fernuni-hagen.de
FernUniversitaet Hagen, LG ES, 58084 Hagen (Germany)
tel:+49 2331/987-1166 fax:987-355 http://www-es.fernuni-hagen.de/~jfh

After reading through the Pickaxe book I seem to remember that you can hook
on a variable being set. Could that technique be used here to determine
your answer? If so, it might provide a good FAQ entry.

John.

is not set in the context of a method so there will be set an object
variable from .. which class?

Well, just try it

pigeon% cat b.rb
#!/usr/bin/ruby
class A
   @@a = 12

   def tt
      p @@a
   end

   def self.tt
      p @@a
   end
end

A.tt
A.new.tt
pigeon%

pigeon% b.rb
12
12
pigeon%

Guy Decoux

“Fritz Heinrichmeyer” fritz.heinrichmeyer@fernuni-hagen.de wrote in
message news:86fzyic0nh.fsf@jfh00.fernuni-hagen.de

Here is part of a code snippet posted earlier.

require 'postgres’
module Apache
class RubyRun
@@pg_conn_handler = nil

    def RubyRun.pg_conn
        if !@@pg_conn_handler

.....

I believe that

@@pg_conn_handler = nil

is not set in the context of a method so there will be set an object
variable from … which class?

There was a big overhaul in behavior of class variables (in the
1.6 and 1.7 series) a couple of month ago. Generally speaking,
the new class variable scoping rules is modeled around class
constant rules.

···

$mess =
"There is no such as a ``singleton class’’ class variable,
instead a class/module variable of the surrounding genuine
(non singleton) class or module is created or modified.

"

class Outer
class Inner
end
class << Inner
Const = “Makes sense too!!!”
@@class_var = $mess
def const_mess
puts Const
end
end
def var_mess
puts @@class_var
end
end

make use of const of Singleton class

class Sub < Outer::Inner; end

class Outer

make of use of Outer’s class variable

def Sub.var_mess
puts @@class_var
end
end

Outer.new.var_mess
Sub.var_mess
Sub.const_mess

resulting in


There is no such as a ``singleton class’’ class variable,
instead a class/module variable of the surrounding genuine
(non singleton) class or module is created or modified.

There is no such as a ``singleton class’’ class variable,
instead a class/module variable of the surrounding genuine
(non singleton) class or module is created or modified.

Makes sense too!!!

There was thread about this issue in last month German
mailing list - see for example
http://lists.bttr.org/pipermail/ruby-de/2002-June/000044.html
where I to explain the exact same question …

/Christoph

i did not recognize the two @@'s. Sorry, kind of Attention Deficit
Syndrom.

···


Fritz Heinrichmeyer mailto:fritz.heinrichmeyer@fernuni-hagen.de
FernUniversitaet Hagen, LG ES, 58084 Hagen (Germany)
tel:+49 2331/987-1166 fax:987-355 http://www-es.fernuni-hagen.de/~jfh