Error reading a Ruby string if the process started with arguments

I have the strangest problem. This code below works fine:

require 'json' require 'net/http' h = Net::HTTP.new("localhost", 4567) while(l = gets.chomp!) res = h.post("/api/v1/service/general",l) puts res.body end 

However, with a slight modification to obtaining the host / port from the parameters:

 require 'json' require 'net/http' h = Net::HTTP.new(ARGV[0], ARGV[1]) while(l = gets.chomp!) res = h.post("/api/v1/service/general",l) puts res.body end 

.. and starting with ruby service.rb localhost 4567 ...

I get this error:

 service.rb:4:in `gets': No such file or directory - localhost (Errno::ENOENT) 

Using ruby ​​1.9.2p0 on Ubuntu 11.04

+6
source share
2 answers

Have you tried while (l = $stdin.gets.chomp!) ? Otherwise, Kernel#gets read from ARGV .

+12
source

Try the following:

 h = Net::HTTP.new(ARGV.shift, ARGV.shift) while(l = gets.chomp!) 

It will fail if you pass more than two arguments. You should call ARGV.clear after building h if you want to handle it.

+1
source

Source: https://habr.com/ru/post/894435/


All Articles