From: Calamitas Date: 2008-06-20T19:10:04+09:00 Subject: Re: Why metaclasses? On Fri, Jun 20, 2008 at 10:48 AM, James Coglan wrote: > Singleton classes cannot be 'included' either, which is to say they cannot > be subclassed. > > irb(main):006:0> class SillyArray < Array > irb(main):007:1> end > => nil > irb(main):009:0> s = SillyArray.new [1,2,3,4] > => [1, 2, 3, 4] > irb(main):011:0> s.class > => SillyArray > irb(main):012:0> s.class.superclass > => Array > irb(main):013:0> class MetaArray < [].metaclass > irb(main):014:1> end > TypeError: can't make subclass of virtual class > from (irb):13 > from :0 > > What I'm trying to get at is: given that you can't really do anything > 'classy' with a metaclass, it seems they could quite easily be modules > instead, and this makes more sense to me as they are just objects that store > methods. Why do they need to be this specific type of module (i.e. Class)? I don't know why exactly --only Matz knows-- but IMO the choice is somewhat arbitrary because whichever is chosen, class or module, you have to take away some of its typical behaviors to have it be singletonsomething. My feeling is that we're better off having singleton *classes*. Modules can appear multiple times in the class hierarchy and because of that some of the methods on Module behave in a much less useful way (IMO) than the corresponding methods on Class, especially when navigating the class hierarchy using #ancestors for instance. Note that modules also suffer from the double/dynamic inclusion problem, so things like Object#extend, or class << a ; include M ; end would need some extra code to make things work like they do now. Just remembered this has been discussed before; take a look here: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/267106 Peter