N
Neil Curzon
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
=> #<Class:String>
irb(main):002:0> String.metaclass.superclass
=> #<Class:Class>
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
=> #<Class:String>
irb(main):002:0> String.metaclass.superclass
=> #<Class:Object>
irb(main):003:0> String.metaclass.superclass == Object.metaclass
=> true
Also, figure 24.3 suggests that for an instance of String, the
metaclass should be a virtual class extending String itself.
Once again only JRuby produces the expected result.
$ irb -r metaid
irb(main):001:0> String.new.metaclass.superclass == String
=> false
$ jirb -r metaid
irb(main):001:0> String.new.metaclass.superclass == String
=> true
Something else I found weird is that when I go up the inheritance of a
String instance's metaclass in plain ruby, I quickly run into a loop:
$ irb -r metaid
irb(main):001:0> c = String.new.metaclass.superclass.superclass
=> #<Class:Class>
irb(main):002:0> c == c.superclass
=> true
Any help in understanding this would be greatly appreciated!
Thanks
Neil
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
=> #<Class:String>
irb(main):002:0> String.metaclass.superclass
=> #<Class:Class>
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
=> #<Class:String>
irb(main):002:0> String.metaclass.superclass
=> #<Class:Object>
irb(main):003:0> String.metaclass.superclass == Object.metaclass
=> true
Also, figure 24.3 suggests that for an instance of String, the
metaclass should be a virtual class extending String itself.
Once again only JRuby produces the expected result.
$ irb -r metaid
irb(main):001:0> String.new.metaclass.superclass == String
=> false
$ jirb -r metaid
irb(main):001:0> String.new.metaclass.superclass == String
=> true
Something else I found weird is that when I go up the inheritance of a
String instance's metaclass in plain ruby, I quickly run into a loop:
$ irb -r metaid
irb(main):001:0> c = String.new.metaclass.superclass.superclass
=> #<Class:Class>
irb(main):002:0> c == c.superclass
=> true
Any help in understanding this would be greatly appreciated!
Thanks
Neil