Use string in file name

I am trying to put a file name from a string but cannot.

This works well:

#!/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." 

But this is not so:

 #!/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." 

Console:

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 'from C: /Ruby22-x64/lib/ruby/2.2.0/open-uri.rb: 36: in open' from launcher.rb:10:in block in 'from launcher.rb: 7: in each_line' from launcher.rb:7:in

+4
source share
1 answer

The problem is with the new char string, try deleting it.

 my_file = open(line.chomp, 'wb') 
+1
source

All Articles