Splats and tuples

    The passed arguments become a Tuple in the method’s body:

    1. # elements is Tuple(Int32, Int32, Int32)
    2. sum 1, 2, 3
    3. # elements is Tuple(Int32, Int32, Int32, Float64)
    4. sum 1, 2, 3, 4.5

    Arguments past the splat parameter can only be passed as named arguments:

    1. def sum(*elements, initial = 0)
    2. total = initial
    3. elements.each do |value|
    4. total += value
    5. end
    6. total
    7. end
    8. sum 1, 2, 3 # => 6
    9. sum 1, 2, 3, initial: 10 # => 16

    Two methods with different required named parameters overload between each other:

    1. def foo(*elements, x)
    2. end
    3. def foo(*elements, y)
    4. 2
    5. end
    6. foo x: "something" # => 1
    7. foo y: "something" # => 2

    The splat parameter can also be left unnamed, with the meaning “after this, named parameters follow”:

    1. def foo(x, y, *, z)
    2. end
    3. foo 1, 2 # Error, missing argument: z
    4. foo 1, 2, z: 3 # OK

    A double splat () captures named arguments that were not matched by other parameters. The type of the parameter is a NamedTuple:

    1. def foo(x, **other)
    2. # Return the captured named arguments as a NamedTuple
    3. other
    4. end
    5. foo 1, y: 2, z: 3 # => {y: 2, z: 3}
    6. foo y: 2, x: 1, z: 3 # => {y: 2, z: 3}

    A NamedTuple can be splat into a method call by using **:

    1. def foo(x, y)
    2. x - y
    3. end
    4. tuple = {y: 3, x: 10}
    5. foo **tuple # => 7