How to override a method with an unknown number of arguments?

What am I doing:

class Foo
  alias_method :__bar__,:bar
  def bar(*arg)
    #do something
    __bar__(*arg)
  end
end

Is this the right way to do this?

Or is it better to find out how many required, optional, block arguments the original method has and write exactly the same number of arguments?

+3
source share
1 answer

Using slurpy arg is the right approach, but you should also handle any block arguments:

class Foo
  alias_method :__bar__,:bar
  def bar(*arg, &block)
    #do something
    __bar__(*arg, &block)
  end
end
+2
source

All Articles