NoMethodError: undefined method `each' for true:TrueClass

Trying to use a method upto and place result into an array.

the below code loops through and run the curl command 6 times and place
results in “result” but its not an array.

1.upto(6) do |num|

puts num

result = system( “curl -Is -k --proxy
https://server#{num}” )
p result.inspect

I assume since the stdout is in the “result” then I could read through

$c = 0
$bad = “”
1.upto(6) do |num|
puts num
result = system( “curl -Is -k --proxy
https://server#{num}” )

puts result

result.each do |line|
if line.include? “200”
$c += 1
puts $c
elsif line.to_s.include?(“”)
$c += 1
puts $c
$bad += “#{line}\n”
puts $bad

I get the following error and any not sure what I am doing wrong? Or
maybe I am going about it all wrong.

NoMethodError: undefined method each' for true:TrueClass from (irb):32:inblock in irb_binding’
from (irb):28:in upto' from (irb):28 from /usr/local/rvm/rubies/ruby-1.9.3-p551/bin/irb:12:in

You are invoking Kernel::sysem

result = system(....)

which, according to, stores into
your variable ‘result’ either true, false or nil. In your case, the
system call succeeds, and ‘result’ contains true.

Next, you are doing

result.each .....

Since ‘result’ is true, and true is a value of class TrueClass, Ruby
searches an ‘each’ method in TrueClass, but doesn’t find one. Hence the

In your case, you should process the output of a shell command. You can
do this by

result=%x(curl -Is -k --proxy


Different to your original solution, everythin printed to stdout (by the
curl command) would NOT visible on the scrren, but stored into ‘result’.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs