On Saturday, September 6, 2003, 1:11:40 PM, Thomas wrote:

> Hey!  It works!  That's so neat, thanks!

> Any chance you'd care to explain a little bit, what I just did??? ;-)


STDOUT is an IO object which is the implicit target of your printing
activities.

Notice you could generalise your dot-printing to write to any IO, but
default to STDOUT (standard output) like so:

  def print_dots(io=STDOUT)
    io.sync = true
    25.times do
      io.print "."
      sleep 1
    end
    io.puts
  end

  print_dots                          # goes to STDOUT
  print_dots(STDOUT)                  # ditto
  print_dots(File.new("abc", "w"))    # goes to file "abc"
  File.open("abc", "w") do |file|     # ditto, but the file will be closed
    print_dots(file)
  end


Anyway, to answer your real question, IO objects have a "sync" method
which synchronises their output, instead of buffering it.

Another way to ensure your output is not buffered is to explicitly
"flush" it.  This may better style than using "sync" because it
doesn't interfere with other parts of the program.

  def print_dots(io=STDOUT)
    25.times do
      io.print "."
      io.flush
      sleep 1
    end
    io.puts
  end


HTH,
Gavin



> "Date" goes:

>> [me:]
>> 
>> > Anybody know how to get my idle little toy to work the way I want it to.
>> > It would help me scratch this little itch way up on top my brain.
>> 
>> Make this as the first line of your program:
>> 
>> STDOUT.sync = true
>> 
>> And see what happens .......................
>> 
>>