-2

I have a base class with child classes that override a method that takes multiple arguments.

class Parent
  def foo *bar
  end
end

class Child < Parent
  def foo bar, baz
  end
end

This works fine. However, suppose there is a method foobar in Parent that calls foo:

def foobar *foo_args
  foo foo_args
end

This raises a ArgumentError when called on a Child instance because foo_args is one single array, while Child.new.foo expects two objects. Is there a way around this?

Leo Brito
  • 2,053
  • 13
  • 20
  • 2
    I cannot reproduce this. Your code works fine for me in Ruby 2.1 and 1.8.7. As far as I know, you're wrong about method signatures having to match between parents and children. There is no such requirement in Ruby. This works fine: http://pastebin.com/g5VgzKPX – user229044 Jan 14 '15 at 19:19
  • Exactly - in Ruby it's just a matter of run-time message dispatching. – WarHog Jan 14 '15 at 19:37
  • 1
    It is not at all clear what you are trying to do. – sawa Jan 14 '15 at 19:45
  • Correct, I was wrong about the signature. However, calling the method from the parent is still an issue. I'll edit the question to clarify. – Leo Brito Jan 14 '15 at 19:47

1 Answers1

1

Your question is not clear, but I think this may be what you want:

def foobar *foo_args
  foo(*foo_args)
end

Still, Child.new.foo must take exactly two arguments for it to not raise an error.

sawa
  • 165,429
  • 45
  • 277
  • 381