A method signature defines the valid calling form of a Ruby method: which arguments it accepts, how they bind to parameters, and which values are optional. section 2.4.1
Ruby signatures can express several argument shapes:
- Required positional parameters demand exactly one argument each; supplying too many or too few raises ArgumentError. section 2.4.1
- A splat parameter such as *args accepts zero or more additional arguments and binds them as an array. section 2.4.1
- Default values make positional parameters optional while allowing a supplied argument to override the default. section 2.4.1
- Keyword parameters use names at the call site; required keywords must be supplied, while defaults make keywords optional. section 9.3.8
- Double-splat parameters such as **options collect undeclared keyword arguments into a hash; signatures may also bind a code block with &block. section 9.3.8
Required, catch-all, and default arguments
def two_or_more(a, b, *c) p a, b, c end two_or_more(1, 2, 3, 4) # 1 # 2 # [3, 4] def default_args(a, b, c = 1) p a, b, c end default_args(3, 2) # 3 # 2 # 1 section 2.4.1
Signatures also matter when intercepting unknown messages. An overridden method_missing receives the missing method name first and uses *args for the remaining arguments; the method name arrives as a symbol. section 4.3
>> def m(a:, b:)
>> p a,b
>> end
=> :m
>> m(a: 1, b: 2)
1
2
=> [1, 2]
Inspect a minimal keyword-argument signature and its matching call. section 9.3.8







