Using keyword arguments
suggest changeYou define a keyword argument in a method by specifying the name in the method definition:
def say(message: "Hello World")
puts message
end
say
# Hello World
say message: "Today is Monday"
# Today is Monday
You can define multiple keyword arguments. The definition order is irrelevant:
def say(message: "Hello World", before: "<p>", after: "</p>")
puts "#{before}#{message}#{after}"
end
say
# <p>Hello World</p>
say message: "Today is Monday"
# <p>Today is Monday</p>
say after: "</p><hr>", message: "Today is Monday"
# <p>Today is Monday</p><hr>
Keyword arguments can be mixed with positional arguments:
def say(message, before: "<p>", after: "</p>")
puts "#{before}#{message}#{after}"
end
say "Hello World", before: "<span>", after: "</span>"
# <span>Hello World</span>
Mixing keyword arguments with positional arguments was a very common approach before Ruby 2.1, because it was not possible to define required keyword arguments.
Moreover, in Ruby < 2.0, it was very common to add a Hash at the end of a method definition to use for optional arguments. The call syntax is very similar to keyword arguments, and a method that takes a trailing Hash still accepts that call.
def say(message, options = {})
before = options.fetch(:before, "<p>")
after = options.fetch(:after, "</p>")
puts "#{before}#{message}#{after}"
end
# The method call is syntactically equivalent to the keyword argument one
say "Hello World", before: "<span>", after: "</span>"
# <span>Hello World</span>
Note that passing an undefined keyword argument raises an error:
def say(message: "Hello World")
puts message
end
say foo: "Hello"
# ArgumentError: unknown keyword: :foo
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents