Arguments: splats and kwargs

Collect with *

A splat parameter collects extra positional arguments into one array.

Callers pass any count of numbers, and the method iterates them as usual.

def total(*numbers) sum = 0 numbers.each do |n| sum += n end sum end puts total(1, 2, 3)

ruby splat_demo.rb

6

Forward with ...

Three dots forward every argument, including blocks, to another method untouched.

The wrapper adds logging while compute keeps the real logic and the original signature.

def logged(...) puts "Calling" compute(...) end def compute(a, b) a + b end puts logged(2, 3)

ruby forward_demo.rb

Calling
5

Keyword arguments

Keyword arguments label each value at the call site, so long parameter lists stay readable.

Callers use the name colon syntax, and Ruby raises when a required key is missing.

def greet(name:, greeting: "Hello") puts "#{greeting}, #{name}" end greet(name: "Ada") greet(name: "Ada", greeting: "Hi")

ruby kwargs_demo.rb

Hello, Ada
Hi, Ada

Defaults and required kwargs

Defaults make arguments optional, while a bare keyword with no default stays required.

Localhost and the default port fill in automatically, but timeout must always be given.

def connect(host: "localhost", port: 5432, timeout:) puts "#{host}:#{port} (#{timeout}s)" end connect(timeout: 5)

ruby connect_demo.rb

localhost:5432 (5s)

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a *numbers parameter collect?

    Answer

    One array of the positional arguments

    The splat gathers every extra positional argument into an array.

  2. What does this program print? def total(*numbers) sum = 0 numbers.each { |n| sum += n } sum end puts total(1, 2, 3)

    Answer

    6

    The method adds the three numbers and returns the sum.

  3. What does total() print for the same method?

    Answer

    0

    The splat collects nothing, and the running sum stays at its start value 0.

  4. What do the three dots in def logged(...) forward?

    Answer

    Every argument, the block included

    Argument forwarding passes the whole call on to the next method.

  5. What does this program print? def logged(...) puts "Calling" compute(...) end def compute(a, b) a + b end puts logged(2, 3)

    Answer

    Calling / 5

    The wrapper prints its line, and compute returns the sum that puts prints.

  6. What does a call to connect(timeout: 5) print for def connect(host: "localhost", port: 5432, timeout:)?

    Answer

    localhost:5432 (5s)

    The two defaults fill in, and the required timeout is the one given.

  7. What happens when a required keyword is left out?

    Answer

    ArgumentError: missing keyword

    A bare keyword with no default must be given, and the error names it.

  8. Which keyword is optional in def connect(host: "localhost", port: 5432, timeout:)?

    Answer

    host and port

    The default makes host and port optional; the bare timeout stays required.

  9. What does this program print? def greet(name:, greeting: "Hello") puts "#{greeting}, #{name}" end greet(name: "Ada")

    Answer

    Hello, Ada

    The default greeting is used when the caller skips it.

  10. Does the order of the keyword arguments at the call site matter?

    Answer

    No: each value carries its name

    That is the reason a long parameter list stays readable with keywords.

Article author: Arthur Isaev