From: Robert Klemme Date: 2011-02-24T17:59:50+09:00 Subject: Re: Q: How to call parent method explistly? On Wed, Feb 23, 2011 at 1:09 AM, Gary Wright wrote: > > On Feb 22, 2011, at 6:25 PM, makoto kuwata wrote: > >> Is it possible to call parent method specifying method name explicitly >> like super.foobar() or parent::foobar()? > > There are a couple of alternatives. > > 1) Create an alias of the parent's method and call it instead: > > class Child < Parent >  alias parent_hello hello >  def _hello(name) >    parent_hello(name) >  end > end > > 2) use a method object to reference the parent's method: > > class Child < Parent >  def parent_hello(*args) >    Parent.instance_method(:hello).bind(self).call(*args) >  end > >  def _hello(name) >    parent_hello(name) >  end > end Folks, why are you making this so complicated? Nobody mentions the standard way of doing this by using "super": class Parent def hello_same_args(a) puts a end def hello_different_args puts "We don't need args" end end class Child < Parent def hello_same_args(a) super puts "now in child" end def hello_different_args(x) super() puts "Child got #{x}" end end irb(main):022:0> Child.new.hello_same_args "ARG" ARG now in child => nil irb(main):023:0> Child.new.hello_different_args "ARG" We don't need args Child got ARG => nil Only if you want to call a _different_ method in the super class you need to use one of the other approaches. But if you think about it, usually this is not needed. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/