Required keyword arguments were introduced in Ruby 2.1, as an improvement to keyword arguments.
To define a keyword argument as required, simply declare the argument without a default value.
def say(message:)
puts message
end
say
# => ArgumentError: missing keyword: message
say message: "Hello World"
# => "Hello World"
You can also mix required and non-required keyword arguments:
def say(before: "<p>", message:, after: "</p>")
puts "#{before}#{message}#{after}"
end
say
# => ArgumentError: missing keyword: message
say message: "Hello World"
# => "<p>Hello World</p>"
say message: "Hello World", before: "<span>", after: "</span>"
# => "<span>Hello World</span>"