Return vs puts


I just completed the Omit words/ refactoring sections. I dont understand the difference between return and push and when to use which. Please explain.

Thank you!


Replace this line with your code.

<do not remove the three backticks above>


puts is a print statement that does not have any side effect.

put string => puts

return is how we send data back to the caller and is not equatable to print or puts.

def foo              # the method  (sometimes referred to as callee)
    "Hello World!"

print foo            # the caller
 ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
 Hello World!        # the return value
=> nil

The above has some unseen magic… First off, it is a parameterless method, hence no (), and second it has an implicit return. The string is returned and printed by the caller.

I have a general rule about printing inside a function (method)… Don’t, unless that is the intended purpose of the method. returning data to the caller preserves it for further use. Printing it in the function with no return value and the data is lost (save for output to the display).

def product(a, b)
    puts a * b                    # <-  42

product(6, 7)

The code is valid, yes, but what if the method is way up the listing somewhere and the reader encounters the caller? It is not very explicit what to expect from that call.

def product(a, b)
    return a * b

puts product(6, 7)                # <-  42

Now we expect to see something printed to the display.

def product(a, b)
    a * b

meaning_of_life = product(6, 7)
puts meaning_of_life              # <-  42


I think I understand what you are saying but I don’t follow your example.
In your example, “def foo” you are saying that the return value is nil - so
when is it necesary to write return in the code?



nil is the return value of the print function. Hello World! is the return value of the foo method.

When we do not want the last line of a method to be returned, we can add a return to the bottom. It could return nothing (which translates to nil) or false or 0 or any value. The caller should not be expecting a return value, though.

Take a look at the exercise in this module relating to first n primes.

require 'prime'   # This is a module. We'll cover these soon!

def first_n_primes(n)
  return "n must be an integer." unless n.is_a? Integer
  return "n must be greater than 0." if n <= 0
  Prime.first n

puts first_n_primes(10)

The last line returns implicitly, the other two, explicitly. But do we even need the other two?

def first_n_primes(n)
  "n must be an integer." unless n.is_a? Integer
  "n must be greater than 0." if n <= 0
  Prime.first n

puts first_n_primes(-1)

Console error…

attempt to take negative size

puts first_n_primes(2.61)
[2, 3]

Weird, it’s like it ignored the conditional altogether. In reality, the first return value was overwritten by the last. When we restore the return keyword,

n must be greater than 0.


n must be an integer.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.