From the course: Complete Guide to Ruby
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Output vs. return values - Ruby Tutorial
From the course: Complete Guide to Ruby
Output vs. return values
- [Instructor] In this movie, I want us to consider the differences between methods that perform output and methods that return values. We already know that methods can output values directly. We can have a line inside the method that calls puts or print. Or methods can return values, and then other Ruby code can call puts or print to perform the output. Is there a difference between these? Let me show you a simple example. Let's say that we have our subtraction method. Notice that inside the subtraction method, it's calling puts. So it's performing the subtraction and then outputting the results inside the method. So when we call it, we just call subtract, and it does both for us. Or we can have our subtract method that performs the calculation, but we don't actually output it until the code that's after it. So we call it and then we output the results. These may seem similar, but the second one is much more flexible. We have the ability to perform this calculation and then do other…
Contents
-
-
-
-
-
-
-
-
-
(Locked)
Define and call methods4m 28s
-
(Locked)
Variable scope3m 36s
-
Arguments5m 46s
-
(Locked)
Argument default values3m 48s
-
(Locked)
Using a hash of options3m 38s
-
(Locked)
Return value3m 56s
-
Return multiple values3m 43s
-
(Locked)
Output vs. return values2m 47s
-
(Locked)
Challenge: Pig Latin2m 32s
-
(Locked)
Solution: Pig Latin8m 12s
-
(Locked)
-
-
-
-
-
-
-
-
-
-
-
-