- Identify implicit return values in Ruby syntax
- Recognize the explicit
return
keyword
As you learned when studying expressions, Ruby returns a value when it evaluates an expression. This is called, suitably, the return value of an expression.
Methods also have return values. Let's study how they work.
Ruby uses something called implicit return which means that the last expression in a method's implementation is the return value of the method itself.
def a_method(a, b)
puts "hi"
a + b
end
a_method(1,2) #=> 3
In the implementation, we didn't have to assign the return value to a
variable. All you have to do is put an expression right before the end
and
ruby will evaluate it. In other programming languages like JavaScript you
must explicitly say:
{
...
return 1 + 2
}
But Ruby likes for you to be able to type less and realizes that in most
methods there's a return value. So you don't have to type the word return
.
Yay!
Be very careful. Recall that the return value of puts
and print
is nil
.
A VERY COMMON BUG FOR NEW RUBYISTS IS THE FOLLOWING:
def a_method(a,b)
puts "I got #{a}"
puts "I got #{b}"
sum = a + b
puts "I got #{sum}"
end
a_method(2,3) #=> nil (<==== Surprising?!)
# Prints:
# I got 2
# I got 3
# I got 5
It's very easy to see the error here, especially if you pay attention to the
return values of your expressions. The return value of puts
is always nil
!
Because Ruby methods always "return" the return value of the last expression (
or whatever you explicitly return with the return
keyword),
this method, which is returning a sum, actually returns nil
.
To avoid this bug, our code would have to look like:
def a_method(a,b)
puts "I got #{a}"
puts "I got #{b}"
sum = a + b
puts "I got #{sum}"
sum
end
a_method(2,3) #=> 5 (<=== What we expect)
# Prints:
# I got 2
# I got 3
# I got 5
It flies against millennia of human evolution, but what you see is not always what is being returned. Put another way: Seeing something printed on the screen by a method is no guarantee that it is the return value of that method.
Code | Return Value |
---|---|
"Hello world" |
"Hello world" |
6 + 3 |
9 |
instructor = "Tim" |
"Tim" |
total = 5 + 4 |
9 |
puts "hello world" |
nil |
print "hello world" |
nil |
Moment for Meta-Learning: If these return values are surprising or don't make sense, test things out in IRB and / or review lessons in Programming as Conversation Part 1.
As you might recall from Programming as Conversation 2: p
prints as well as
returns the input. That might be the right tool, depending on your situation.
Speaking of explicit return like in JavaScript (and Java, and Python for that matter!) Ruby does have an explicit return command. Rubyists typically use it to exit early from a method with a specific return value.
Let's take a look:
def stylish_chef
best_hairstyle = "Guy Fieri"
return "Martha Stewart"
"Guy Fieri"
end
What do you expect the return value of the above method to be? Using IRB, copy and paste the above method and call it.
You may have expected the return value to be "Guy Fieri". His name is the last
line of the method. However, the return value of the above method is actually
=> Martha Stewart
!
The return
keyword will disrupt the execution of your method. If you employ
it, your method will return whatever you have explicitly told it to (in this
case, "Martha Stewart"
), and then stop the remainder of the lines won't even
be seen!
The explicit use of the return
keyword is generally avoided by many Rubyists,
but there are instances where you might want to use return
instead of relying
on implicit returns. What if you decided that a slow calculation could be avoided
if a simple condition were true. This pattern is known as a "guard clause" if you
want to be very stylish. It helps you avoid an if...else...end
conditional.
def get_stock_market_data(date)
return nil if is_a_weekend?(date)
# Imagine an expensive, slow calculation hereafter
end
To see the value of this, we'll need to learn more about collection data types,
but that's the subject of our next module. Additionally, sometimes the return
values of some of the basic Ruby methods might return something that is unpredictable.
Using return
explicitly lets you make sure your method always returns what
you need.
Knowing how methods return values is crucial as you'll be using them constantly
in programs both big and small. Knowing the difference between puts
and
print
, and return values will help you avoid a common pitfall.
Return values are how different parts of your program communicate with one
another. You don't have to worry too much about this for now, but as you start
to build more complicated programs, you'll find that the return value of one
method might be operated on by a subsequent method. Don't forget to watch out
for those nil
s!