[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

sleep command in iterators (silly n00b toy question

Thomas Yager-Madden

9/6/2003 1:35:00 AM

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
5 Answers

Shashank Date

9/6/2003 1:52:00 AM

0


"Thomas Yager-Madden" <tym@NOSPAMsyntactician.com>

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


Thomas Yager-Madden

9/6/2003 3:08:00 AM

0

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

Gavin Sinclair

9/6/2003 3:29:00 AM

0

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


Sean O'Dell

9/6/2003 3:57:00 AM

0

Thomas Yager-Madden wrote:

> 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

Thomas Yager-Madden

9/6/2003 4:56:00 AM

0


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.