2

I have a ruby bash script to download a zip file and output a progress bar to the stdout. I have the following

# Temp file name
tmp = ActiveSupport::SecureRandom.hex(8)
file = temp_dl_dir+tmp+'.zip'

print file.inspect

# Download
progress_bar = nil
open(file, 'w', :content_length_proc => lambda { |length|
  if length && 0 < length
    progress_bar = ProgressBar.new('...', length)
    progress_bar.file_transfer_mode
  end 
},
:progress_proc => lambda { |progress|
  progress_bar.set(progress) if progress_bar
}) do |fo|
    fo.print open(dl).read
end

But when I run it I get

open-uri.rb:32:in `initialize': can't convert Hash into Integer (TypeError)
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:32:in `open_uri_original_open'
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:32:in `open'
    from ./site.rb:191 (line 191 is the open(file, 'w' ...) one)

Which means there is a problem with my open(file, 'w' ... function

I can't figure out what's wrong +_+

6
  • I was just about to ask the same thing. Which line is the stack trace complaining about? Commented Jun 28, 2012 at 21:53
  • Line 191 is the open(file, 'w', ...) one... Line 32 is def is_dir?(directory) Commented Jun 28, 2012 at 21:53
  • Oh I wonder if the problem is with the third argument in open? The third argument in open is ":content_length_proc => lambda..." which is a hash. The stack trace is talking about open-uri.rb:32. So maybe that call to open is expecting the third argument to be an Int. Commented Jun 28, 2012 at 21:56
  • is_dir? has nothing to do with this, the 32 is open-uri.rb line 32, do you want me to post open-uri.rb gem code? I can do that if you want, but I didn't write it. The proble is line 191, which is the open(file, 'w' line Commented Jun 28, 2012 at 21:59
  • @NiklasB.: The error that the stack trace mentions is not in his file. It is in open-uri.rb which is a ruby lib for opening a file at a URI. Commented Jun 28, 2012 at 21:59

1 Answer 1

4

The :content_length_proc appears to be related to OpenURI::OpenRead#open. (Check by ri open then search for :content_length_proc.)

My ri documentation on this method says:

However, mode must be read mode because OpenURI::OpenRead#open doesn't support write mode (yet). Also perm is ignored because it is meaningful only for file creation.

So you cannot use OpenURI::OpenRead#open with 'w' mode. You must either use a different mechanism to write to your URL if that is your actual goal. (Your English description says you're trying to download a file, but you wouldn't be using 'w' mode to the OpenURI::OpenRead#open method in that case...)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.