Ruby Return Statement Tutorial

In this section, we will learn what the return statement is and how to use it in Ruby.

Note: we’re assuming you’re already familiar with the Methods in Ruby.

What is Return Statement in Ruby?

In Ruby, a method is capable of returning a value to its caller (when the method is being called).

For example, you might have a method that runs multiplication operation. So you want to return the result of multiplying two or more values to the caller of the method.

Alright, now let’s see how a method can return a value.

Types of Returning Value in Ruby:

A method can return a value in two ways:

  • Explicitly returning value using the return statement
  • Implicit return value

Ruby return statement

Using the `return` statement, we can return a value from a method.

The return keyword takes one value on its right side and that is the value we want to return to the caller of the method.

Note that the return statement will also terminate the work of its method. That means when your program reaches to this statement, it will close the method and get back to the caller of the method with the return value of the method. So you can’t put any statement after the `return` statement and expect it to run!

Example: using the return statement in Ruby

def math_operation (type, first_num, second_num)
    if type == "division"
        return first_num / second_num
    elsif type == "multiply"
        return first_num * second_num

res = math_operation("division",10,5)

puts res



In the body of the `math_operation` we have an if elsif statement where the value of the type parameter is checked and based on that value either a multiplication or a division operation happens.

We also used two return statements in the body of this method. For this example, because the value of the `type` is set to `division`, then the body of the `if` statement runs and so the result of dividing the two values will be returned from the method.

Also note that this method is called in a way so that its result is assigned to the `res` variable.

That’s how the `res` variable got the value 2.

Ruby implicit return value

In Ruby, the last expression of a method is considered as its return value as well! This is called implicit return value!

Example: implicitly returning value in Ruby

def sayHi 
    puts "This message is coming from the sayHi method. "
    "Hello to you 🙂 "

result = sayHi

puts result


This message is coming from the sayHi method.

Hello to you 🙂

If we look at the last statement of the sayHi method, we can see that it’s a string value. This is actually the implicit value that will be returned when the method is called.

So here we called the method and passed the result (the string value) to the `result` variable.

That’s how this variable is now contains the string value of the `sayHi` method.

When to use the return statement in Ruby?

You might be thinking why and when to use the explicit return statement then?

Well, it’s always the last expression of the method that is considered as the return value of the method. This means there’s no way to return a value in the middle of the method (like in the body of an if statement in a method)

But using the return statement gives us this ability to terminate a method in the middle of its work and return a value as a result. Basically, using the return statement, we are not limited to just the last statement of a method anymore!


Top Technologies