Skip to content

Instantly share code, notes, and snippets.

@ngpestelos
Last active December 24, 2015 05:19
Show Gist options
  • Save ngpestelos/6749799 to your computer and use it in GitHub Desktop.
Save ngpestelos/6749799 to your computer and use it in GitHub Desktop.
lab rat
# see Metaprogramming Ruby, p. 108
class C
def a_method
"C#a_method"
end
def eigenclass
class << self; self; end
end
end
class D < C;
def self.a_class_method
'D.a_class_method'
end
end
obj = D.new
class << obj
def a_singleton_method
'obj#a_singleton_method'
end
end
class C
class << self
def a_class_method
'C.a_class_method'
end
def eigenclass
class << self
self
end
end
end
end
puts "*** no eigenclass ***"
puts obj.class # D
puts obj.class.superclass # C
puts obj.a_method # C#a_method
puts obj.a_singleton_method # obj#a_singleton_method
puts obj.class.a_class_method # D.a_class_method
puts C.a_class_method # C.a_class_method
puts obj.class.superclass.a_class_method # C.a_class_method
puts "*** with eigenclass ***"
puts obj.eigenclass # #<Class:#D>
puts obj.eigenclass.a_class_method # D.a_class_method
puts obj.eigenclass.superclass # #<Class:D>
puts C.eigenclass # #<Class:C>
puts C.eigenclass.superclass # #<Class:Class>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment