×

ruby · level 1 · unit 7 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

hands the answer back so you can keep it.

So far your methods use puts. That shows a value on the screen, and then it is gone. Your program cannot catch it, keep it or do maths with it.

To return a value, write return and the value. The call then turns into the answer, as if double(4) were the number 8. Store it, show it or use it again.

Ruby also hands back a method's last line by itself, so many Ruby coders leave return out. Careful: puts hands back nil, so a method that ends with puts gives you nothing.

def double(number)
  return number * 2
end
answer = double(4)
puts answer
puts double(answer)

puts is showing someone your answer, and return is handing it to them.