← Home · Ruby skill tree

An interactive Ruby lesson

Methods

A method gives a name to code you can run again. Build on Variables to pass values into a method and use the value it returns.

p(...) is a method you have used to display values. It displays an inspected value, with quotes around strings. Predict the output, press Run, then open the expected output to check your answer. Edit the code and run it again. Reset restores that example; Stop ends a running example.

The first Run downloads Ruby into your browser. JavaScript and a network connection are needed to run examples. Each run starts fresh, so define every method and variable in the editor you run. The code and answers remain readable without JavaScript.

1. Define a method and call it

def show_total defines a method named show_total. The indented code is its body, and end closes the definition. Defining the method does not run its body. The call show_total() runs it. Predict the order of the output lines.

Show expected output
"Before"
5
"After"

Try it: Remove the show_total() line. Predict what still prints. Restore it, then add a second call.

2. Pass a value into a method

A parameter is a name in a method definition that receives a value from a call. Here number is the parameter. An argument is the value you pass: 4 is the argument in show_double(4). Predict both results.

Show expected output
8
14

Try it: Change the second argument from 7 to 0. Predict which output line changes.

3. Match arguments to parameters in order

Commas separate parameters and arguments. In show_difference(9, 4), first receives 9 and second receives 4. These are positional arguments: their order determines which parameter receives each value. Predict both differences.

Show expected output
5
-5

Try it: Swap the arguments in the first call. Predict both lines again.

4. Return a value to the caller

A returned value is the result that a method call gives back to the code that called it. By default, Ruby returns the value of the last expression evaluated in the method body. Here add_three returns a number without displaying it.

The first call below produces no output. In p(add_three(4)), Ruby calls add_three first, then passes its returned value to p for display. Predict how many lines print.

Show expected output
7

Try it: Replace p(add_three(4)) with add_three(4). Both calls still return 7, but neither displays it.

A method can both display and return a value. With one argument, p returns that same value after displaying it. The methods in the first three examples therefore return the number they display.

5. Use the last expression's value

A method body can contain several expressions. Here Ruby calculates subtotal, then adds 2. That final expression determines the returned value. The assignment stores it in total. Predict the number displayed.

Show expected output
14

Try it: Replace subtotal + 2 with subtotal. Predict the new value of total.

6. Store and reuse a returned value

Assign a returned value to a variable to use it later. You can also pass it into another call. Here add_bonus(updated) returns a new number. That call does not reassign updated. Predict all output lines.

Show expected output
7
9
7

Try it: Change starting to 0. Predict all output lines before running.

Practice on your own

Replace the code in the last editor with a program for these tasks. Predict the output, then run it.

  1. Define total_price(price, count). Make it return price * count without displaying anything.
  2. Call it with arguments 4 and 3. Assign the returned value to total.
  3. Use p to display total, then total + 2.
  4. Change the count argument to 5. Predict both results before running.
Show one solution and its output
def total_price(price, count)
  price * count
end
total = total_price(4, 3)
p(total)
p(total + 2)

Output:

12
14

Ruby documentation

The Ruby 4.0 documentation covers method definitions, parameters, and return values and calling methods with arguments. The documentation for p explains its output and returned value.

Return to the Ruby skill tree to choose another topic.