From: Sam Sungshik Kong Date: 2004-06-23T01:33:13+09:00 Subject: Re: A question about Class and Object I don't think it's a stupid explanation...:) It's just above my intelligence. I'll keep learning Ruby and someday I'll understand it. Thanks. Sam "ts" wrote in message news:200406220934.i5M9Y7r02023@moulon.inra.fr... > >>>>> "S" == Sam Sungshik Kong writes: > > S> I'm still confused with class and object and metaclass... > > well, try to forget about metaclass. > > I'll try to give you a *STUPID* explanation > > ruby has only objects, this mean that a class is an object and like any > object it has a class (Class) where are defined its methods. > > This mean that, for example, the method ::new is defined in Class, > something like this > > class Class > def new(*args, &block) > obj = allocate > obj.send(:initialize, *args, &block) > obj > end > end > > This work fine, but you see that it exist a problem. If it was easy to > define ::new, it will be more difficult to define ::allocate in the same > way, because Array::allocate is completely different from Hash::allocate > and you don't want to define a method Class#allocate with a big switch and > change this method each time that you introduce a new class > > Another problem if that it will be nice if you can define a method > File::open, which take a block. You can't define this method in Class > (i.e. Class#open) because this mean that you'll define also Array::open, > Hash::open and you don't want this. > > To solve these 2 problems, ruby associate with each class an object and > when it search a class method it search first in this object then it look > in Class. > > Now ruby can work, because > * the method ::allocate will be defined in this special object, and you > still have Class#new > > * you can define in the object associated with File, the method ::open > and only this class will have this method > > But a class is a little special because there is inheritence and when you > write > > class A < Array > end > > you want to re-use, for A, the method ::allocate which was defined in the > special object associated with Array > > This mean that for this special object, associated with a class, you want > * the possibility to define method > * the possibility to use inheritence > > an object is well adapted to do this, this is precisely a class and > because this class will always be associated with only *one* other object > it will called a "singleton" class > > Finally this give this (where (A) is the singleton class associated with > A) > > Class < Module < Object > (Class) < (Module) < (Object) < Class > > A < Array < Object > (A) < (Array) < (Object) > > and you have the schema which is given in object.c > > Now, at the beginning, I've said that a class is an object and I've > introduced the singleton class. This mean that you can associate a > singleton class with any object, and this will give methods specifics to > this object, for example > > a = [] > class << a > def tt > end > end > > When ruby will search a method for `a', it will first search in its > singleton class then in its class. > > > > Guy Decoux > >