Created
December 20, 2011 04:34
-
-
Save bvsatyaram/1500272 to your computer and use it in GitHub Desktop.
print vs puts in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prints five dots in a single line | |
5.times do | |
print "." | |
end | |
# Prints five dots in five different lines | |
5.times do | |
puts "." | |
end | |
# Acts as expected. | |
# Prints five dots in five different lines, with a time interval of 2 seconds between each of the prints. | |
5.times do | |
puts "." | |
sleep 2 | |
end | |
# The following acts a bit strange | |
# Waits for ten seconds without any out put. Then prints five dots at once. | |
5.times do | |
print "." | |
sleep 2 | |
end | |
# This can be solved easily by flushing out $stdout | |
# Prints five dots in a single line with a time interval of 2 seconds between each print | |
5.times do | |
print "." | |
$stdout.flush | |
sleep 2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment