Skip to content

Instantly share code, notes, and snippets.

@timbogit
Created March 8, 2012 05:19
Show Gist options
  • Save timbogit/1998878 to your computer and use it in GitHub Desktop.
Save timbogit/1998878 to your computer and use it in GitHub Desktop.
Musing on the Ruby object model in Ruby 1.8.7 vs. 1.9.3
obj = Object.new
puts "obj is #{obj}"
eigobj = class << obj
self
end
puts "Eigenclass of obj is #{eigobj}"
puts "Superclass of the eigenclass of obj is #{eigobj.superclass}"
eigclassobj = class << obj.class
self
end
puts "Eigenclass of the class of obj is #{eigclassobj}"
if eigclassobj == eigobj.superclass
# this is true for Ruby 1.8.7
puts "Eigenclass of the class of an object is the same as the Superclass of the Eigenclass of an object"
def eigclassobj.hello
puts "This is a method defined on the Eigenclass of the class of obj, but called on the Superclass the Eigenclass of obj"
end
eigobj.superclass.hello
else
# this is true for Ruby 1.9.3
eigeigobjsuper = class << eigobj
self.superclass
end
puts "Superclass of the eigenclass of the eigenclass obj is #{eigeigobjsuper}"
if eigclassobj == eigeigobjsuper
puts "Eigenclass of the class of an object is the same as the Superclass of the Eigenclass of the Eigenclass obj"
def eigclassobj.hello_again
puts "This is a method defined on the Eigenclass of the class of obj, but called on the Superclass of the Eigenclass of the Eigenclass of obj"
end
eigeigobjsuper.hello_again
end
end
# Ruby 1.8.7
obj is #<Object:0x10505b148>
Eigenclass of obj is #<Class:#<Object:0x10505b148>>
Superclass of the eigenclass of obj is #<Class:Object>
Eigenclass of the class of obj is #<Class:Object>
Eigenclass of the class of an object is the same as the Superclass of the Eigenclass of an object
This is a method defined on the Eigenclass of the class of the object, but called on the Superclass the Eigenclass of the object
# Ruby 1.9.3
obj is #<Object:0x007ff60a1dffa8>
Eigenclass of obj is #<Class:#<Object:0x007ff60a1dffa8>>
Superclass of the eigenclass of obj is Object
Eigenclass of the class of obj is #<Class:Object>
Superclass of the eigenclass of the eigenclass obj is #<Class:Object>
Eigenclass of the class of an object is the same as the Superclass of the Eigenclass of the Eigenclass obj
This is a method defined on the Eigenclass of the class of the object, but called on the Superclass of the Eigenclass of the Eigenclass of the object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment