Ruby Return Values

Carly Ann Pecora
May 8 · 3 min read

Ruby is a very accessible, easy-to-read programming language. When I started learning Ruby, I found it very programmer-friendly. However, one thing continuously tripped me up: Ruby return values. Every time I called a method, two values would appear in my terminal! Which was the right value, and what did they mean?

> def say_hello
> puts "Hello World"
> end

If I call my method above, the result will look like this:

> say_hello
Hello World
=>nil

Where did that nil come from?

Even more frustrating was the output of the first ruby iterator I learned, each:

> array = [1, 2, 3]
> array.each do |num|
> num * 2
end
=> [1, 2, 3]

Wait, I just performed an operation on each number in my array, and nothing happened? I get back the same value I put in, even though I tried to change the data?

Turns out, every method in ruby returns an object. One object. If nothing has been explicitly returned, i.e. the return keyword is not used in a method (return is hardly ever used in Ruby), then a method returns the last line executed. For example :

> def add_two(num)
> num + 2
> end

When called:

> add_two(6)
=>8

The add_two return value is the last line executed in the method.

But wait, if I go back to my say_hello method, I see the last line executed is puts “Hello World”, but my return value is still nil? This is because the puts method was designed to do one thing only: print an argument to the console. It will not and will never have a return value, because that’s just the way puts was created.

Going back to the previous each iteration, I see that I have obviously executed a line of ruby that is not reflected in my return value.

> array = [1, 2, 3]
> array.each do |num|
> num * 2
end
=> [1, 2, 3]

Ruby totally ignored my num * 2 operation! Not cool.

Turns out, the each iterator is just that: an iterator. Nothing fancy. Each is used to go from one element to the next in an array, and that’s about it. This is beneficial when we need to access an array’s data without trying to manipulate that data.

> array = [1, 2, 3]
> array.each do |num|
> puts num
> end
1
2
3
=>[1, 2, 3]

Each was used to jump from element to element, simply putsing to the terminal each element, and the original array is returned.

What’s the significance of a return value? What’s the point? A return value allows a programmer to work off the method being called. For example:

> num = 8
> num.even?
> true

The method even? is called and returns a boolean: true or false. I can write more code based on the return value of even?

> num = 8> if num.even?
> puts "I have an even number!"
> else
> puts "I have an odd number!"
> end
I have an even number!
=>nil

I was able to build off the return value of even?! Pretty cool, if you ask me! If I want to get a bit more complicated, I can set a variable equal to a method and use that variable somewhere in my code! The value of the variable will equal the return value of its corresponding method. For example:

> def add_two(num)
> num + 2
> end
=> :add_two #this is just showing that I've built a method
>
> def multiply_two(num)
> num * 2
> end
=> :multiply_two
>
> new_number = add_two(2)
=>4
> multiply_two(new_number)
=>8

Return values become easier to understand over time and allow a programmer to build methods off other methods; they are practical, convenient, and extremely useful. Going forward, I hope you are able to understand the importance of return values and the role they play in Ruby.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade