barlop
barlop

Reputation: 13753

how can I make a ruby program that sends to stdout in real time like copy does?

I don't have a great way to word this question's title. But I think I can explain what i'm asking for quite clearly.

The title of the question is, How can I make a ruby program that sends to stdout in real time like copy does?

I'll explain what I mean.

Suppose in one command prompt I do

C:\Windows\System32>md e:\exes

C:\Windows\System32>copy *.exe e:\exes >c:\carp\f.f

C:\Windows\System32>

Then from another command prompt I do

C:\carp>type f.f

I then see the f.f file build up, as the copy progresses.

and once the copy is complete, then f.f has the full stdout

However, this is not the case with my ruby program

Here is my ruby program

E:\rubylig>type putsandoutput.rb
20.times do
 puts "a"
  sleep 1
end

E:\rubylig>ruby putsandoutput.rb >a.a

If I then open another cmd prompt and do

E:\rubylog>type a.a

The a.a file is blank, until the putsandoutput.rb program has run to completion.

Then the a.a file is full.

But i'd like my ruby program to, like copy, be able to have its output redirected to the file, as it runs, rather than waiting till it completes.

Is it possible to do that. If so how, and if not then why not?

If not then i'm guessing it's a limitation of Ruby..

It's not a command line limitation, since 'copy' can do it.

Upvotes: 0

Views: 53

Answers (1)

Phlip
Phlip

Reputation: 5343

You may be asking how to "flush" stdout. I thought that puts would flush automatically, so maybe it does not. Try $stdout.flush.

Upvotes: 4

Related Questions