-
Book Overview & Buying
-
Table Of Contents
Polished Ruby Programming - Second Edition
By :
Earlier in the chapter you learned about the anonymous rest parameter (*), anonymous keyword rest parameter (**), and anonymous block parameter (&). You can use these to forward all positional arguments, keyword arguments, and block to another method:
def a(*, **, &) = b(*, **, &)
def b(*a, **kw) = [a, kw, yield]
a(1, c: 2){3}
# => [[1], {c: 2}, 3]
However, Since Ruby 2.7, there has been an easier way to forward all arguments, using argument forwarding:
def a(...) = b(...)
This provides the same behavior as using the three anonymous parameters, but in a simpler syntax. You can also use this syntax with leading and/or optional parameters:
def a(callable, ...) = callable.(...)
def a(callable=to_proc, ...) = callable.(...)
When calling a method, you can use argument forwarding with leading arguments:
def a(...) = b(:c, ...)
However, for more advanced cases, such as when you are using post parameters and/or keyword parameters, you cannot use argument...
Change the font size
Change margin width
Change background colour