can't flush print when download open-uri

Discussion in 'Ruby' started by T. Onoma, Nov 12, 2003.

  1. T. Onoma

    T. Onoma Guest

    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
    T. Onoma, Nov 12, 2003
    #1
    1. Advertising

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

    "T. Onoma" <> schrieb im Newsbeitrag
    news:...
    > 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
    Robert Klemme, Nov 12, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Harris
    Replies:
    0
    Views:
    6,362
    Simon Harris
    May 10, 2005
  2. Stanimir Stamenkov
    Replies:
    1
    Views:
    2,461
    Stanimir Stamenkov
    Aug 17, 2005
  3. Pavel
    Replies:
    2
    Views:
    1,634
    Peter Flynn
    Aug 4, 2004
  4. etheriau
    Replies:
    1
    Views:
    665
    Pavel
    Aug 23, 2004
  5. Jay 99
    Replies:
    2
    Views:
    185
    Jay 99
    Apr 4, 2009
Loading...

Share This Page