Is there a clean, good, easy way to do the following:
I suspect the entry below will fail in at least one
of those subjective categories
class Human; end
class Male < Human
def laugh
puts "Yo ho ho"
end
end
class Female < Human
def laugh
puts "Tee hee"
end
end
module Yokel
def initialize @meth = method(:laugh)
self.class.class_eval do
define_method(:laugh) do
puts "Slapping my knee" @meth.call
puts "Snorting"
end
end
end
end
class Human
include Yokel
end
m = Male.new
m.laugh
#=> "Slapping my knee"
#=> "Yo ho ho"
#=> "Snorting"
f = Female.new
f.laugh
#=> "Slapping my knee"
#=> "Tee hee"
#=> "Snorting"
module Yokel
def initialize @meth = method(:laugh)
self.class.class_eval do
define_method(:laugh) do
puts "Slapping my knee" @meth.call
puts "Snorting"
end
end
end
end
Shorter (as long as __send__ handles private method calls) ...
module Yokel
def initialize @meth = method(:laugh)
self.class.__send__(:define_method, :laugh) do
puts "Slapping my knee" @meth.call
puts "Snorting"
end
end
end