使用字符串

时间:2015-08-11 14:26:35

标签: ruby

我正在尝试从字符串中输入文件名,但不能。

这很有效:

#!/usr/bin/ruby
require 'httpclient'

http2 = HTTPClient.new
response = http2.get_content("http://example.com/version.ini")

response.each_line do | line |

    http = HTTPClient.new
    my_file = open('file.zip', 'wb')
    my_file.write(http.get_content("http://example.com/data.zip"))

end

puts "Done."

但这不是:

#!/usr/bin/ruby
require 'httpclient'

http2 = HTTPClient.new
response = http2.get_content("http://example.com/version.ini")

response.each_line do | line |
    puts line # -> file.zip
    http = HTTPClient.new
    my_file = open(line, 'wb')
    my_file.write(http.get_content("http://example.com/data.zip"))

end

puts "Done."

控制台:

  

C:/Ruby22-x64/lib/ruby/2.2.0/open-uri.rb:36:in initialize': Invalid argument @ rb_sysopen - file.zip (Errno::EINVAL) from C:/Ruby22-x64/lib/ruby/2.2.0/open-uri.rb:36:in open'           来自C:/Ruby22-x64/lib/ruby/2.2.0/open-uri.rb:36:in open' from launcher.rb:10:in阻止'           来自launcher.rb:7:each_line' from launcher.rb:7:in'

1 个答案:

答案 0 :(得分:1)

问题是使用换行符char,尝试剥离它。

my_file = open(line.chomp, 'wb')