On Aug 21, 2005, at 3:11 AM, Alexandru Popescu wrote:
> Coming from the Java world, for me a class method is a (static)  
> method available withouth a self (this) reference, but it is  
> available/callable from any instance. (is this available in Ruby?)

Sorry, I don't know Java (any more). Are you describing a global  
function that is only available to instance methods of a certain  
class, which is invoked without an explicit receiver?

Something like:
class Foo
     def foo_m
         x = my_utility_function( )
     end
end

class Bar
     def bar_m
         x = my_utility_function( )
     end
end

f = Foo.new
f.foo_m #works

b = Bar.new
b.bar_m #raises an error because my_utility_function() isn't  
available to Bar instances


> It seems that in Ruby the class method is a method belonging to the  
> class ClassExample object.

Yup, that's the case. Classes are objects too. So you can do:

class Foo
     def self.upcount   #same as def Foo.upcount
         @instances ||= 0
         @instances += 1
     end

     def self.getcount
         @instances
     end


     def initialize
         self.class.upcount
     end

     def count_neighbors
         self.class.getcount
     end
end

f1 = Foo.new
f2 = Foo.new
f3 = Foo.new
p f1.count_neighbors #=> 3


As you can see above, the Class has its own scope for its own  
instance variable. The class itself is a completely separate instance  
of the "Class" class, which just happens to be tied to instances of  
it in a few special ways.