Methods

Define and call

The keyword def opens a method and end closes it. Method names use snake case, with words joined by underscores. Call the method by writing its name, with or without parentheses.

def greet puts "hello" end greet

ruby greet.rb

hello

Arguments

Names listed in parentheses become required arguments, and the call must supply the same count. Inside the body, each argument behaves like an ordinary local variable.

def add(a, b) puts a + b end add(2, 3)

ruby add.rb

5

Defaults and kwargs

A default after the equals sign applies when the caller skips that argument. Keyword arguments carry names at the call site, so their order no longer matters. Use defaults for simple options and keywords when a method takes several settings.

def greet(name = "guest") puts "hello #{name}" end greet greet("Ada") def move(x:, y:) puts "#{x}, #{y}" end move(x: 1, y: 2)

ruby greet_options.rb

hello guest
hello Ada
1, 2

Implicit return

Ruby returns the last evaluated expression, so most methods need no explicit return. The keyword return exits early with a value, which suits guard clauses. A method with nothing useful to give back returns nil.

def double(n) n * 2 end puts double(4) def sign(n) return "zero" if n == 0 "other" end puts sign(0)

ruby double.rb

8
zero

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How is a method defined?

    Answer

    With def and end

    def opens the method and end closes it, as in every example on the page.

  2. What does this program print? def greet puts "hello" end greet

    Answer

    hello

    The call runs the body, and puts prints its line.

  3. What does this program print? def add(a, b) puts a + b end add(2, 3)

    Answer

    5

    The two arguments are added inside the body and printed.

  4. What happens when a required argument is left out of the call?

    Answer

    ArgumentError: wrong number of arguments

    A required argument must be supplied, and the error names the expected count.

  5. What does this program print? def greet(name = "guest") puts "hello #{name}" end greet

    Answer

    hello guest

    The default applies when the caller skips the argument.

  6. What does a keyword argument give the call site?

    Answer

    A name for each value, so their order does not matter

    move(x: 1, y: 2) names both values, and move(y: 2, x: 1) is the same call.

  7. What does a method return without an explicit return?

    Answer

    The last evaluated expression

    Ruby returns the last expression, which is why most methods need no return.

  8. What does this program print? def double(n) n * 2 end puts double(4)

    Answer

    8

    The last expression is the result, and puts prints it.

  9. What does this program print? def sign(n) return "zero" if n == 0 "other" end puts sign(0)

    Answer

    zero

    The guard returns zero before the last expression is reached.

  10. What does a method with nothing useful to give back return?

    Answer

    nil

    A body whose last expression is a puts call returns nil.

Article author: Arthur Isaev