From: dblack@... Date: 2006-06-24T06:49:25+09:00 Subject: Re: class << self? Hi -- On Sat, 24 Jun 2006, Erik Veenstra wrote: > 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. Classes are objects, though (as you point out later in a slightly different context), so they can store data: class C @x = 1 end > 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. Namely, singleton classes :-) (I know, I know.) > Every Object can have a classes of its own where it can store code. > That's the trick. Those terms aren't interchangeable, though; a class can be anonymous without being the singleton class of an object: Class.new Singleton classes are anonymous, but anonymous classes aren't always singleton classes. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) See what the readers are saying about "Ruby for Rails"! http://www.rubypowerandlight.com/quotes