From: "David A. Black" Date: 2007-11-08T20:18:44+09:00 Subject: Re: Metaclasses Hi -- On Thu, 8 Nov 2007, Neil Curzon wrote: > Hi, all. I'm trying to understand chapter 24 of Programming Ruby. > I'm using "metaid.rb" from why the lucky stiff's site to help with the > examples. (http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html) > > Everything is pretty straightforward except for the diagrams of > inheritance of metaclasses. Figure 24.2 indicates that for a direct > subclass of Object, its metaclass's superclass should be Object's > metaclass. When I try this out in irb, I don't get the expected > result. > > $ irb -r metaid > irb(main):001:0> String.metaclass > => # > irb(main):002:0> String.metaclass.superclass > => # > irb(main):003:0> String.metaclass.superclass == Object.metaclass > => false > > Surprisingly, when I try it in JRuby, I do get the expected result. > > $ jirb -r metaid > irb(main):001:0> String.metaclass > => # > irb(main):002:0> String.metaclass.superclass > => # > irb(main):003:0> String.metaclass.superclass == Object.metaclass > => true It actually depends which version of Ruby you use. The superclass thing worked as expected in 1.8.2; then it stopped working (I'm not sure why); and in 1.9 it's working again. ("Not working" doesn't mean that the subclasses couldn't call the methods defined in their superclasses' singleton classes, just that the superclass thing itself was not in place.) $ cat super.rb class Object def singleton_class class << self; self; end end end p String.singleton_class.superclass == Object.singleton_class $ /usr/local/lib/ruby-1.8.2/bin/ruby -v super.rb ruby 1.8.2 (2004-12-25) [i686-linux] true $ ruby -v super.rb ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux] false $ /usr/local/lib/ruby-svn/bin/ruby -v super.rb ruby 1.9.0 (2007-11-07 patchlevel 0) [i686-linux] true David -- Upcoming training by David A. Black/Ruby Power and Light, LLC: * Advancing With Rails, Edison, NJ, November 6-9 * Advancing With Rails, Berlin, Germany, November 19-22 * Intro to Rails, London, UK, December 3-6 (by Skills Matter) See http://www.rubypal.com for details!