Skip to content

Latest commit

 

History

History
45 lines (33 loc) · 1006 Bytes

forward-all-arguments-to-another-method.md

File metadata and controls

45 lines (33 loc) · 1006 Bytes

Forward All Arguments To Another Method

There are three types of arguments that a Ruby method can receive. Positional arguments, keyword arguments, and a block argument.

A method that deals with all three might be defined like this:

def forwarding_method(*args, **kwargs, &block)
  # implementation
end

Now lets say we have some concrete method that we want to forward these arguments to:

def concrete_method(*args, **kwargs)
  x = args.first || 1
  key, y = kwargs.first || [:a, 2]

  puts "Dealing with #{x} and key #{key}: #{y}"

  yield(x, y)
end

We could forward arguments the longhand way like this:

def forwarding_method(*args, **kwargs, &block)
  concrete_method(*args, **kwargs, &block)
end

However, since Ruby 2.7 we have access to a shorthand "triple-dot" syntax for forwarding all arguments.

def forwarding_method(...)
  concrete_method(...)
end

source