S
S
sergeyxx12xx2016-02-20 08:50:07
ruby
sergeyxx12xx, 2016-02-20 08:50:07

Problem with system statement in ruby?

1 #ruby
2 #url = gets.chomp.to_s
3 url = "mail.ru"
4 qa = system "curl -Is #{url} |head -n1 | grep -Eo '[0-9]{3,3 }'"
5 if (200 <= qa and qa <= 299)
6 puts 'OK'
7 elsif (300 <= qa and qa <= 399)
8 puts 'redirect'
9 elsif (400 <= qa and qa <= 499)
10 puts 'Error Client'
11 elsif (500 <= qa and qa <= 599)
12 puts 'Server Error'
13 elsif (qa != 0-9 or qa <= 600 or 199 <= qa)
14 puts ' Time OUT'
15 end
Output:
ruby ​​test.na.dostupnost
302
test.na.dostupnost:5:in `<=': comparison of Fixnum with true failed (ArgumentError)
from test.na.dostupnost:5:in `'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitri Sinitsa, 2016-02-20
@sergeyxx12xx

system does not return the result of the command execution. In your case, it just returns TRUE all the time.
If you want to execute a command and return the result of its execution, then use this: ruby-doc.org/core-2.3.0/Kernel.html#method-i-60

url = gets.chomp.to_s 
qa  = `curl -Is #{url} |head -n1 | grep -Eo '[0-9]{3,3}'`.to_i

case qa
when 200..299 then puts 'OK'
when 300..399 then puts 'Redirect'
when 400..499 then puts 'Client Error'
when 500..599 then puts 'Server Error'
else 
  if qa != 0-9 || qa <= 600 || 199 <= qa
    puts 'Timeout'
  else
    puts 'Unknown error occurred'
  end
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question