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 trialdsaf
5,926 PointsWhy Jason uses 'print' instead of 'puts'?
I tried both:
(a) print "Please enter your name: "
and
(b) puts "Please enter your name: "
As I saw in my console and this first answer in this website , 'print' does not add a new line to the end of the output.
Are there any reasons to use 'print' instead of 'puts' beside the new-line reason?
2 Answers
Jeremy Woodbridge
25,278 Pointsputs is short for (put string) where as print will print the line of code along with variable, methods and functions to the console. Basically if you just need to print a string puts is great but if there is more involved print will be your best friend
Maciej Czuchnowski
36,441 PointsHere's a quick example of use and difference:
Kenan Memis
47,314 PointsIs it an old post, so could be ruby has some changes since then? I just tried at my console the both examples and both works the same way. Results are as follows (all dots in both examples are printed with 2 seconds intervals:
2.1.0 :001 > 5.times do
2.1.0 :002 > puts "."
2.1.0 :003?> sleep 2
2.1.0 :004?> end
.
.
.
.
.
=> 5
2.1.0 :005 > 5.times do
2.1.0 :006 > print "."
2.1.0 :007?> sleep 2
2.1.0 :008?> end
..... => 5
Maciej Czuchnowski
36,441 PointsThis is possible. Or the buffering is working differently under different conditions.
If they now work the same, this is even better, because now you can do the 'waiting' dots with sleep properly :)