Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Collections Build a Grocery List Program Build a Grocery List Program: Part 1

Thomas Minnick
Thomas Minnick
2,539 Points

I don't get the difference between Puts, Return, and Print ?

Whenever They are used in the video i don't understand the difference between them.

1 Answer

Ari Misha
Ari Misha
19,323 Points

Hiya Thomas!

puts adds a newline to the end of each argument if there is not one already.

print does not add a new line.

For example:

puts [[1,2,3], [4,5,nil]] Would return: 1 2 3 4 5

Whereas print [[1,2,3], [4,5,nil]] would return:

[[1,2,3], [4,5,nil]]

Notice how puts does not output the nil value whereas print does.

Now "Return" is something different from both "print" and "puts". It doesnt print anything to the console. What does it do then? Well, it "returns" a value which will be available to use later throughout your code. In your career as a programmer, you use "puts" or "print" when you wanna output a string but you're going to use "return" a lot like a lot when you're using functions to add different functionality to your application.

And Ruby does an implicit "return", means if the last statement returns something , you dont have to write unnecessary "return" keyword over and over coz Ruby knows what needs to be returned. I hope it helped. (: