1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-04 23:58:01 +00:00

Add Forward All Arguments To Another Method as a Ruby TIL

This commit is contained in:
jbranchaud
2024-11-16 12:32:21 -06:00
parent 877537228f
commit 543a82730d
2 changed files with 47 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
# 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:
```ruby
def forwarding_method(*args, **kwargs, &block)
# implementation
end
```
Now lets say we have some concrete method that we want to forward these
arguments to:
```ruby
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:
```ruby
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.
```ruby
def forwarding_method(...)
concrete_method(...)
end
```
[source](https://ruby-doc.org/3.3.6/syntax/methods_rdoc.html#label-Argument+Forwarding)