sleep command in iterators (silly n00b toy question)

  • Thread starter Thomas Yager-Madden
  • Start date
T

Thomas Yager-Madden

So, still busy just getting introduced to this ruby business. For
riduculous reasons entirely my own, I decide it would neat-o to have a
programm write a line of dots on the screen (.....), but to write them
slooowly, maybe a second between each dot. Okay. So I try

25.times { print "."; sleep 1 }
print "\n"

But instead of printing a dot, waiting a second, and then printing
another dot, &c. Ruby waits 25 seconds and then prints 25 dots all at
once.

Interestingly, { puts "."; sleep 1 } shows the desired behavior of
waiting a second between each dot, but of course it puts each dot on a
separate line.

It's looking to me like Ruby gathers up print messages, and saves them
up, writing to stdout only when it encounters a newine or the end of the
program.

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.

Thanks
Thomas
 
S

Shashank Date

Thomas Yager-Madden said:
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 .......................
 
T

Thomas Yager-Madden

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

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

- thos.


"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 .......................
 
G

Gavin Sinclair

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 .......................
 
S

Sean O'Dell

Thomas said:
Hey! It works! That's so neat, thanks!

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

- thos.


"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 .......................

The I/O for stdout is line-buffered. It buffers everything until a
newline is sent.

Sean O'Dell
 
T

Thomas Yager-Madden

Gavin Sinclair goes:
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.

Not to mention, invoking a method called "flush" always adds a bit of
that "programming should be fun again" principle ;-)

Thanks a bunch! This explains more than it seems to on the surface.

*bows to sensei*

Cheers,
Thos.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top