Return Values: What Comes Out

You may have noticed that some methods give you something back when you call them. For example, you say gets returns a string (the string you typed in), and the + method in 5+3 (which is actually 5.+(3)) returns 8. The arithmetic methods for numbers return numbers, and the arithmetic methods for strings return strings.

It’s important to understand the difference between a method returning a value (returning it to the code that called the method), and your program outputting text to your screen, like puts does. Notice that 5+3 returns 8; it does not print 8 on your screen.

So, what does puts return? We never cared before, but let’s look at it now:

1: return_val = puts ​"This puts returned:"
2: puts return_val ...

Get Learn to Program, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.