Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > can't flush print when download open-uri

Reply
Thread Tools

can't flush print when download open-uri

 
 
T. Onoma
Guest
Posts: n/a
 
      11-12-2003
I'm confused. why won't this print? I tried flushing the $defout and $stdout as well as the source_file_io, but that doesn't work either.

# download
prioritized_locations.each do |location|
begin
source_file_io = File.open(self.source_path,'w')
remote_file = open(location)
while incoming = remote_file.read(512)
source_file_io.write(incoming)
# THIS WON'T THIS PRINT!!!!!!!!!!!
print "\ca{source_file_io.pos}KB/#{@source_size}KB"
end
rescue
puts "#{location} failed"
remote_file.close unless remote_file.nil?
source_file_io.close unless source_file_io.nil?
next # try next location
else
puts ' Done.'
break # we got it
ensure
remote_file.close unless remote_file.nil?
source_file_io.close unless source_file_io.nil?
end
end

-t0

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      11-12-2003

Maybe it's because you didn't terminate with "\n".

"T. Onoma" <> schrieb im Newsbeitrag
news:E1AJs2a-0004hd-...
> I'm confused. why won't this print? I tried flushing the $defout and

$stdout as well as the source_file_io, but that doesn't work either.

I'd try $defout.sync=true then you don't need the flushes.

> # download
> prioritized_locations.each do |location|
> begin
> source_file_io = File.open(self.source_path,'w')
> remote_file = open(location)
> while incoming = remote_file.read(512)
> source_file_io.write(incoming)
> # THIS WON'T THIS PRINT!!!!!!!!!!!
> print "\ca{source_file_io.pos}KB/#{@source_size}KB"


did you mean

print "#{source_file_io.pos}KB/#{@source_size}KB\n"

?

> end
> rescue
> puts "#{location} failed"


> remote_file.close unless remote_file.nil?
> source_file_io.close unless source_file_io.nil?


Closing here is superfluous since "ensure" takes care of that.

> next # try next location


superfluous as well.

> else
> puts ' Done.'
> break # we got it


superfluous as well.

> ensure
> remote_file.close unless remote_file.nil?
> source_file_io.close unless source_file_io.nil?
> end
> end


In the end we get much cleaner code:

require 'open-uri'

# download
$defout.sync = true

prioritized_locations.each do |location|
begin
File.open(self.source_path,'w') do |source_file_io|
open(location) do |remote_file|
while incoming = remote_file.read(512)
source_file_io.write(incoming)
print "#{source_file_io.pos}KB/#{@source_size}KB "
end
end
end
rescue => e
$stderr.puts "#{location} failed: #{e}"
else
puts 'Done.'
end
end

This works for me - and does print.

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Direct Download Movies - No Download Limits - Download DivX DVDMovies hussain dandan Python 0 12-06-2009 04:52 AM
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
How to flush print Jay Davis Python 1 03-07-2004 09:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57