Prev: cannot freeze my gems
Next: LOGIN INTO YAHOO
From: Alex DeCaria on 29 Apr 2010 08:13 Thanks everyone. That is very helpful. My remaining questions is "Why isn't Ruby's default state such that STDOUT.sync is always 'true'? In other words, what's the advantage of having standard output buffered rather than instantaneous? In every program I've written in Ruby I've always wanted anything written to standard output to appear instantaneously. --Alex -- Posted via http://www.ruby-forum.com/.
From: hemant on 29 Apr 2010 08:31 On Thu, Apr 29, 2010 at 1:28 PM, Robert Dober <robert.dober(a)gmail.com> wrote: > On Thu, Apr 29, 2010 at 9:16 AM, Siep Korteling <s.korteling(a)gmail.com> wrote: > <snip> >> Instead of STDOUT.flush-ing multiple times, you can set STDOUT.sync = >> true once. > > And, please, instead of using STDOUT, please use $stdout, this allows > your users to mock stdout to somewhere else *without* getting constant > redefinition warnings. One can always use IO#reopen if one wants to redirect the output somewhere. I still prefer constant declaration more than global variable. -- Let them talk of their oriental summer climes of everlasting conservatories; give me the privilege of making my own summer with my own coals. http://gnufied.org
From: Robert Klemme on 29 Apr 2010 12:03 2010/4/29 Alex DeCaria <alex.decaria(a)millersville.edu>: > Thanks everyone. That is very helpful. My remaining questions is "Why > isn't Ruby's default state such that STDOUT.sync is always 'true'? In > other words, what's the advantage of having standard output buffered > rather than instantaneous? In every program I've written in Ruby I've > always wanted anything written to standard output to appear > instantaneously. Maybe because it is more efficient for the general case and there might not be a reliable platform independent way to detect the type of output (terminal, file, pipe...). Btw, different Ruby implementations seem to not agree on the default value: 18:00:33 ~$ allruby -e 'p $stdout.sync' CYGWIN_NT-5.1 padrklemme1 1.7.5(0.225/5/3) 2010-04-12 19:07 i686 Cygwin ======================================== ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] false ======================================== ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-cygwin] false ======================================== jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) Client VM 1.6.0_20) [x86-java] true 18:01:14 ~$ Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Caleb Clausen on 29 Apr 2010 12:29 On 4/29/10, Alex DeCaria <alex.decaria(a)millersville.edu> wrote: > Thanks everyone. That is very helpful. My remaining questions is "Why > isn't Ruby's default state such that STDOUT.sync is always 'true'? In > other words, what's the advantage of having standard output buffered > rather than instantaneous? In every program I've written in Ruby I've > always wanted anything written to standard output to appear > instantaneously. I had thought that stdout (and stderr?) were supposed to be line buffered by default when attached to terminals, else fully buffered. This is traditional unix stdio behavior, nothing to do with ruby per se. Line buffered means that a flush is performed automatically by stdio whenever a \n is printed (which would mean that puts always flushes whatever it prints, since it always appends \n if one was not present). Fully buffered is the same as $stdout.sync==false. 1.8 seems to behave in the way I described, even tho the $stdout.sync flag is false. 1.9 may well behave differently, since it bypasses stdio buffering and does its own thing. Here are the results of two experiments I did on the command line. In the first, stdout is a real file, and clearly is unbuffered, since the output is not printed to it right away. In the second, output is to the terminal, and was printed right away (tho you can't see that here, you'll have to take my word for it). $ ruby -e 'puts "foo"; sleep 15; p $stdout.sync' > foop & sleep 1; ls -l foop [2] 6521 -rw-r--r-- 1 caleb caleb 0 2010-04-29 09:23 foop $ cat foop foo false $ ruby -e 'puts "foo"; sleep 15; p $stdout.sync' foo false
From: Robert Dober on 29 Apr 2010 13:26
On Thu, Apr 29, 2010 at 2:31 PM, hemant <gethemant(a)gmail.com> wrote: > On Thu, Apr 29, 2010 at 1:28 PM, Robert Dober <robert.dober(a)gmail.com> wrote: >> On Thu, Apr 29, 2010 at 9:16 AM, Siep Korteling <s.korteling(a)gmail.com> wrote: >> <snip> >>> Instead of STDOUT.flush-ing multiple times, you can set STDOUT.sync = >>> true once. >> >> And, please, instead of using STDOUT, please use $stdout, this allows >> your users to mock stdout to somewhere else *without* getting constant >> redefinition warnings. > > > One can always use IO#reopen if one wants to redirect the output > somewhere. I still prefer constant declaration more than global > variable. > Hmm, can you? I am not that sure, here is the behavior I was thinking about: def with_stdout stdout, &blk original_stdout = $stdout $stdout = stdout blk[] ensure $stdout = original_stdout end R. |