Rubyで、クラスの内部から基底クラスのメソッドを呼び出す場合は、 superを使えば良いのですが、 どうしても外部から基底クラスのメソッドを呼び出したい場合には、 以下のようにすれば実現出来ます。
1 >> class Foo; def foo; puts "foo#{self}"; end end 2 => nil 3 >> class Bar < Foo; def foo; puts "bar#{self}"; end end 4 => nil 5 >> Bar.new.foo 6 bar#<Bar:0x25c2d60>
このように、通常は基底クラスのメソッドを直接呼び出す事は出来ませんが、
1 >> Foo.instance_method(:foo).bind(Bar.new).call 2 foo#<Bar:0x25b7 f00>
一度UnboundMet
posted by
genki
on Sun 25 Jan 2009
at 06:43