logo
  • Jobs
  • About Me
  • Contact
  • Home
« Implementing method_missing
Sometimes the problem is in your tests »

puts vs print in ruby

Posted February 11th, 2009 by Matt Berther

I discovered something a bit peculiar about the puts and print methods in Ruby. puts seems to flush immediately, and therefore shows up on $stdout right away. Take the code example below:

5.times {
  puts "."
  sleep 2
}

This functions exactly the way that you would expect. It places a single period on $stdout, followed by a two second pause for five iterations. puts inserts a new line character as well, so instead of placing each period on the same line, each one is on a new line. print does not insert the automatic newline sequence, so it would place each one on the same line. However, the code below does not function the way you would expect.

5.times {
  print "."
  sleep 2
}

The code above waits for 10 seconds and then prints all 5 periods. As it turns out, this is because the print method buffers the output. The easiest way to get around this (for a situation like the above) is to set the sync property on $stdout.

STDOUT.sync = true
5.times {
  print "."
  sleep 2
}

This sets $stdout to avoid buffering the input, which most modern operating systems do. If you find yourself in a situation where you need to have a small amount of output sent immediately to the screen, this is a good technique to utilize to serve this requirement.

7 Comments

This entry was posted on Wednesday, February 11th, 2009 at 7:46 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

roger
February 20th, 2009

Wow that’s cool. i’d imagine it flushes because of the added “\n” at the end? Nice tip

eMancu
March 10th, 2009

Thx for ‘STDOUT.sync = true ‘ because puts and print waits for 10 seconds and then prints 5 dots.

I don’t know why my ruby do somethings like that when you say that is a difference between puts and print.

But all is fixed thx to you!!

PS: Sorry for my english

kiran
May 19th, 2009

Superb tip…..thanks a lot…..

June
September 16th, 2009

I tried on my computer. That’s no problem for prints to print a dot every 2 seconds. And then I check the value of STDOUT.sync, it’s true.
So I think the value maybe vary in different systems. It would be better to check it before writing this kind of codes.

June
September 16th, 2009

it also works for STDOUT.sync=false
why?

TheFu
September 25th, 2009

What happens if you …

5.times {
print “.”
sleep 2
}
puts “:”

On my system, the last ‘puts’ flushes the prior prints.

Matt Berther
September 25th, 2009

What I was really after was that the . printed with the 2 second delay. If the last puts flushes the prior prints, then they all show up at once, instead of at 2 second intervals.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010