Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Ruby methods have a return value, a value they send back to the code that called them. Let's experiment with return values.
- Ruby methods have a return value, a value they send back to the code that called them.
def add(first, second)
return first + second
end
def subtract(first, second)
return first - second
end
total = add(1, 2)
puts total
remaining = subtract(8, 3)
puts remaining
- We don't need the
return
keywords here - the last expression evaluated in a method becomes that method's return value.
def add(first, second)
first + second
end
def subtract(first, second)
first - second
end
total = add(1, 2)
puts total
remaining = subtract(8, 3)
puts remaining
- Pass return values to other functions
# We don't have to assign the return value to a variable first.
# We can just pass it straight to "puts".
puts add(1, 2)
puts subtract(8, 3)
# We can also use method return values as arguments to our own methods.
puts add(add(1, 2), 4)
puts add(3, subtract(11, 7))
puts add(add(1, 2), subtract(11, 7))
Returning a value vs. using puts
- It's important to distinguish between method output using
puts
and a method return value. - This works because
subtract
returns the result of the subtraction:
def subtract(first, second)
first - second
end
number = 9
puts number # 9
number = subtract(number, 1)
puts number # 8
number = subtract(number, 1)
puts number # 7
- This doesn't work because
subtract
returns the result of callingputs
, which is an empty value:
def subtract(first, second)
puts first - second # Returns an empty value!
end
number = 9
puts number # 9
number = subtract(number, 1) # Assigns empty value to "number"!
puts number # Prints empty value!
number = subtract(number, 1) # Tries to subtract from empty value, resulting in error!
puts number # Never runs because of the error!
Widget store code
Here's our widget store code, with the ask
method updated to return what the user types.
def ask(question)
print question
# "gets" returns a string the user types.
# Because the call to "gets" is the last
# expression in the "ask" method, the
# return value of "gets" becomes the
# return value of "ask".
gets
end
puts "Welcome to the widget store!"
answer = ask("How many widgets are you ordering?")
puts answer # Prints whatever the user typed.
Additional practice
We've created a workshop where you can get additional practice defining Ruby methods. Don't miss this chance to strengthen your skills; it will make later stages easier!
Visit the workshop here: Practice Ruby Methods
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up