Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View classes.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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.