Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
module M
def foo
end
end
class BaseClass
def base_method
end
end
class ExtendClass
def other_method
end
extend M
end
b = BaseClass.new
b.extend M
x = ExtendClass.new
# getting the eigenclass
# for ruby >= 2.1.0 is also possible
# ec = b.singleton_class
ec = class << b; self; end
c = b.class
x_ec = class << x; self; end
x_c = x.class
puts
puts "Example: object's Class vs Eigenclass vs Base Class"
puts "Object's Class is: #{c}"
puts "Object's Eigenclass: #{ec}"
puts "Base Class: #{BaseClass}"
puts "Extended Object's Class is: #{x_c}"
puts "Extended Object's Eigenclass is: #{x_ec}"
puts "Extended Base Class: #{ExtendClass}"
puts
puts "They're all classes"
puts "Class of object's Class: #{c.class}"
puts "Class of object's Eigenclass: #{ec.class}"
puts "Class of base Class: #{BaseClass.class}"
puts "Class of extended object's Class: #{x_c.class}"
puts "Class of extended object's Eigenclass: #{x_ec.class}"
puts "Class of extended base Class: #{ExtendClass.class}"
puts
puts "Equality tests:"
puts "object's Class == object's Eigenclass: #{c == ec}"
puts "object's Class === object's Eigenclass: #{c === ec}"
puts "object's Class == base class: #{c == BaseClass}"
puts "object's Class === base class: #{c === BaseClass}"
puts "object's Eigenclass == base class: #{ec == BaseClass}"
puts "object's Eigenclass === base class: #{ec === BaseClass}"
puts
puts "Test presence of method 'foo'"
puts "Object: #{b.methods.include?(:foo)}"
puts "Object's Eigenclass: #{ec.methods.include?(:foo)}"
puts "Object's Class: #{c.methods.include?(:foo)}"
puts "Base class: #{BaseClass.methods.include?(:foo)}"
puts "Extended Object: #{x.methods.include?(:foo)}"
puts "Extended Object's Eigenclass: #{x_ec.methods.include?(:foo)}"
puts "Extended Object's Class: #{x_c.methods.include?(:foo)}"
puts "Extended class: #{ExtendClass.methods.include?(:foo)}"
puts
puts "Test presence of instance method 'foo'"
puts "Object's Eigenclass: #{ec.instance_methods.include?(:foo)}"
puts "Object's Class: #{c.instance_methods.include?(:foo)}"
puts "Base class: #{BaseClass.instance_methods.include?(:foo)}"
puts "Extended Object's Eigenclass: #{x_ec.instance_methods.include?(:foo)}"
puts "Extended Object's Class: #{x_c.instance_methods.include?(:foo)}"
puts "Extended class: #{ExtendClass.instance_methods.include?(:foo)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.