Proto = Class.new(Class)
def Proto.clone
Class.new(self)
end
a = Proto.clone
def a.f
puts 42
end
a.f
b = a.clone
b.f
def b.f
puts 43
end
a.f
b.f
def a.g
"Hello differential inheritance"
end
bool_val = (a.g == b.g)
p bool_val
AnimalSounds = Module.new
AnimalSounds.class_eval %q!
def createSound(attr_name)
class_eval <<-EOS
#def self.#{attr_name}; puts "class method '#{attr_name}' called"; end
def self.#{attr_name}(arg = nil)
str = "class method '#{attr_name}' of class #{self.inspect} called"
arg ? (puts str + " with " + arg.to_s) : (puts str)
end
EOS
end!
puts
p AnimalSounds
p AnimalSounds.class
p AnimalSounds.class.superclass
p AnimalSounds.class.ancestors
p AnimalSounds.singleton_methods
puts
Pet = Object.clone.extend(AnimalSounds)
Pet.createSound("meow")
Pet.meow
Pet.meow("argument")
p Pet
p Pet.class
p Pet.class.superclass
p Pet.class.ancestors
p Pet.singleton_methods
puts
fido = Pet.clone
fido.meow
fido.meow("argument")
fido.createSound("woof")
fido.woof
fido.woof("argument")
puts
fido2 = fido.clone
fido2.woof
fido2.woof("argument")
fido2.createSound("woof2")
fido2.woof2
fido2.woof2("argument")
puts
p Pet.singleton_methods
p fido.singleton_methods
p fido2.singleton_methods
puts