From: "Chr. Rippel" Date: 2002-01-18T09:57:40+09:00 Subject: Re: Metaclasses "Phil Tomson" wrote in .... > Some of the descriptions of Ruby I've seen mention that Ruby has > metaclasses. I've read the section on metaclasses in the Pickaxe book, > but I still have questions: > *What can metaclasses do for me? > *How can I use them? To make a long story short meta classes are responsible for the lookup path of class_methods and in doing so they are already working hard for you. Each Ruby object comes with a #type and (normally not instanciated) #singleton_type, which are both Class objects). The singleton_type is a subtype the normal ``type'' but is quite different from normal sub-typing (i.e. inheritance) - its kind of like vertical versus horizontal inheritance. Anyway an implementation of #singleton_type is class Object def singleton_type class << self; self end end end # for convenience class Class alias :meta_class :singleton_type end and since both klass,klass.meta_klass have type Class it is unsurprising that they are somewhat special when compared to an ordinary pair object, object.singleton_type. This difference comes to light in method lookup. If you try to invoke object.meth (*args) the preference is normallly is finding a method :meth among the singleton_mehtods of object (controlled by object.sigleton_method) - only if this fails will Ruby check among the instance methods of object.type or if necessary in one of its ``normal superclasses''. If however, object happens to be of type Class this search path becomes much longer ---- class Class def superclasses res = [self] tmp = self res.push tmp while tmp = tmp.superclass return res end end # For example, if class A; end class B < A; end class C < B; end # then the complete class_method search path is given by puts C.meta_class.superclasses.join(" < ") # => < # < # # < #\ # < Class < Module < Object # which sort of mirrors the an ordinary search path given by # C(==C.new.singleton_type.to_s) < C < B < A < Object puts C.new.singleton_type.superclasses.join(" < ") > In Python-land they seem to be happy that in 2.2 they now have usable > metaclassses and usually they describe them as sort of being like class > templates. Apparently we've had them all along in Ruby, I'm just not sure > how to make use of them (though I get the idea that they are doing things > for me indirectly, in the background). Out of curiosity I would very much appreciate if someone more fluent in Python could translate python meta classes into a Ruby language, if this at all possible. From my limited understanding Ruby's meta classes and Python's meta classes are not the same thing. For example, strictly speaking python apparently has no class methods, or more generally speaking singleton types, on the other hand you apparently can subclass pythons Class (to do so in Ruby requires you to un comment two lines of source code, so it is a very conscious design decision on Matz part not to allow this.) I sort have the feeling that what python people call meta classes is sort of distributed over the whole arsenal of Ruby relective capability and hook methods. /Christoph