Ask coding questions

← Back to all posts
How to I get the terminal to show return values? (Ruby)
landmark (3)

I'm using to work through some Ruby exercises. If I write in the editor, for example...

x = 2+5
puts x

7 will appear in the terminal.

But if I write in the editor...

return x

...nothing appears in the terminal. It runs, because the terminal skips to a new line, but the value does not appear. Why?

landmark (3)

Miguel_dede, adding "puts" before calling the function seems like a decent workaround. Thanks!

Miguel_dede (7)

I would need to see the full example, but return does not print anything on itself.
Also in Ruby the last value in a function is always returned without an explicit return <-- See

timmy_i_chen (1153)

I'm not as familiar with Ruby, but I assume that return in ruby is the same as return in every other language. Generally, return is used in a function to send a value back to its function call. Also, output generally only shows in the console when you explicitly tell it to do so (via puts or p in ruby). Since you aren't telling the program to print anything to the console, it simply doesn't.

If you have a repl we can look at, I can give some more examples.

pythonCoffee (3)

return x in which function? Did you execute the function? Is it even in a function?