Ruby: required keyword arguments in the pre-2.1 era

Posted Over 5 years ago. Visible to the public.

Starting with Ruby 2.0 you can define methods with keyword arguments.

In 2.1+ required keyword arguments can be defined by using a colon without default value:

def match(value, ignore:)
  # ...
end

To accomplish something similar in ruby 1.8, use:

def match(value, options = {})
  ignore = options.fetch(:ignore)
  # ...
end
Daniel Straßner
Last edit
Over 1 year ago
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Daniel Straßner to makandra dev (2018-10-01 11:24)