Skip to content

Instantly share code, notes, and snippets.

@bvsatyaram
Created December 20, 2011 04:34
Show Gist options
  • Save bvsatyaram/1500272 to your computer and use it in GitHub Desktop.
Save bvsatyaram/1500272 to your computer and use it in GitHub Desktop.
print vs puts in Ruby
# 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