Ruby and Return Values

Deirdre Sullivan
2 min readMay 21, 2020

In Ruby, a method always returns one object. The returned object can be one thing but the method can only ever return one object, and it always returns something.

A method can return the object “nil” which means nothing, but it is still an object. If we wanted to return a collection of items at once, we can return an Array that holds the collected items, but the Array itself is just one object.

In Ruby, we do not have to explicitly state “return” for there to be a return value. In fact, in practice, it is hardly used at all.

If we don’t explicitly state a return, a method will return the value that was returned from the last evaluated statement. This is typically the last line in the method body, but not always.

Depending on whether we use the explicit return from the method or not, a method with a similar set of expressions can work in a drastically different way.

In Ruby, there are two different kinds of returns, an explicit return and an implicit return. An explicit return from a method is a return caused by the keyword return. The implicit return from a method is a return that occurs by default, without using the keyword return.

The work of puts and the return keyword are two different things. While a method can only have one return value (one object), if a method contains one or more puts statements, the console will show more than one object. The return value this kind of situation will be denoted by =>, or otherwise known as the hash-rocket.

--

--