[pre lang="ruby"]class A
def self.hello; p 'hello'; end
end
class B < A
def self.hello(t)
self.superclass.class_eval %Q~define_singleton_method :hello do p "#{t}" end~
end
end
A.hello
B.hello('world')
A.hello[/pre]
[pre lang="ruby"]class A
@@a = 1
def self.hello; p @@a; end
end
class B < A
def self.hello; @@a += 1; p @@a; end
end
A.hello
B.hello
A.hello[/pre]
[pre lang="ruby"]
class C
counter = Hash.new(0)
define_singleton_method :method_added do |sym|
return if equal?(C)
value = counter[sym] += 1
C.send(:define_method, sym){value}
end
end