Backo
Backo

Reputation: 18871

How to pass arguments passed in to another method?

I am using Ruby on Rails 3.2.9 and Ruby 1.9.3. I am trying to implement a method in which to run a send method and I would like to pass to the "sent" method all arguments passed to the underling method. That is, given

def method1(arg1, arg2 = true, *args)
  # ...
  self.class.send(:method2, # passing all arguments passed to method1, whatever those are)
end

then I would like to pass all arguments passed to the method1 (in this case, arg1, arg2 = true, *args) to the method2.

How should I make that? For example, is it possible to use Ruby "splat" functionalities with send?

Upvotes: 2

Views: 5148

Answers (1)

mikej
mikej

Reputation: 66263

Your current method signature method1(arg1 = true, arg2, *args) is invalid because the arguments with default values must come after the required arguments if you're also using splat optional arguments. But if you changed this to:

method1(arg1, arg2 = true, *args)

then you can do

self.class.send(:method2, arg1, arg2, *args)

Upvotes: 4

Related Questions