Method Arguments
Methods accept default values, keyword arguments, splat *args and double splat **opts. Keyword arguments make calls readable.
Example
def greet(name, greeting = "Hello", punct: "!") = "#{greeting} #{name}#{punct}"
def sum(*n) = n.sum
def config(**opts) = opts
puts greet("Ada"), greet("Tunde", "Hi", punct: "?")
puts sum(1, 2, 3)
puts config(a: 1, b: 2)Result
Hello Ada!
Hi Tunde?
6
{:a=>1, :b=>2}Exercise
Collect extra arguments.
def f(args) end
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.