On Sat, 2006-06-24 at 05:36 +0900, Roger Johansson wrote:

> thanks, Ive figured this part out.
>
> but what exactly does the class << self do? why do I need it,
> why cant I just do "alias bla,orig" on static methods
> directly?

Data is stored in objects. Code is stored in classes. You can't
store data in classes and you can't store code in objects. It's
just that simple.

But...

Let's have a look at this code:

 foo = Foo.new
 def foo.bar
   # code...
 end
 foo.bar

This adds method bar to foo. Not to Foo, but to "that specific
instance of Foo". Uh, that doesn't match very well with my
first statement about class and objects, does it?

So Matz gave us meta classes, or anonymous classes, or, virtual
classes. Different names for the same thing. Every Object can
have a classes of its own where it can store code. That's the
trick.

You can rewrite the previous code to the following example,
which more explicitly shows the anonymous class:

 foo = Foo.new
 class << foo
   def bar
     # code...
   end
 end
 foo.bar

Since Foo is an object of class Class, it can have it's own
class, too. That's where class methods (static methods for Java
Joe...) go:

 class << Foo
   def bar
     # code...
   end
 end
 Foo.bar

The object (!) Foo has a method of its own, stored in its own
anonymous class and not in class Class (which would affect
other classes as well).

Print the following article, take a good cigar, a good glass of
beer, preferably Dutch beer, and read... Over and over again...

 http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html

gegroet,
Erik V. - http://www.erikveen.dds.nl/