Ruby でサブクラスからスーパークラスのメソッドを明示的に呼び出す
Ruby でサブクラスで上書きしたスーパークラスのメソッドを super
を使う以外で呼び出したい場合、Method#bind
を使用します。
class Super def homu "homu" end end class Sub < Super def homu "homuhomu" end end x = Sub.new x.homu # => "homuhomu" # スーパークラスから method を取得して #bind する x.class.superclass.instance_method(:homu).bind(x).call # => "homu"
Method
が思ったよりも柔軟性高い。