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 Basics (Retired) How Ruby Works Input and Output

dsaf
dsaf
5,926 Points

Why 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

puts 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

Kenan Memis
Kenan Memis
47,314 Points

Is 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
Maciej Czuchnowski
36,441 Points

This 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 :)