BIG ERROR:class or module required for rescue clause (TypeEr

W

W0rtex Indigo

require 'socket'
require 'timeout'
def ping( host, service = "echo",timeout = 2)
begin
timeout( timeout ){
TCPSocket.open( host, service){}
}
rescue Errno::ECONNREFUSED
true
rescue false
else true
end
end
p ping(ARGV[0] || "google.ru")

When host is down i got error:" ping:10:in `ping': class or module
required for rescue clause (TypeError) from ping:14"
when host is alive i've got true
Can you help me please!
 
R

Robert Klemme

require 'socket'
require 'timeout'
def ping( host, service = "echo",timeout = 2)
begin
timeout( timeout ){
TCPSocket.open( host, service){}
}
rescue Errno::ECONNREFUSED
true
rescue false
^^^^^ this is not a class.

Either put a class here or nothing.
else true
end
end
p ping(ARGV[0] || "google.ru")

When host is down i got error:" ping:10:in `ping': class or module
required for rescue clause (TypeError) from ping:14"
when host is alive i've got true
Can you help me please!

The question is, what do you want to achieve?

Kind regards

robert
 
R

Rick DeNatale

require 'socket'
require 'timeout'
def ping( host, service = "echo",timeout = 2)
begin
timeout( timeout ){
TCPSocket.open( host, service){}
}
rescue Errno::ECONNREFUSED
true
rescue false
else true
end
end
p ping(ARGV[0] || "google.ru")

When host is down i got error:" ping:10:in `ping': class or module
required for rescue clause (TypeError) from ping:14"
when host is alive i've got true
Can you help me please!

It's complaining about
rescue false

Maybe something like:

def ping( host, service = "echo",timeout = 2)
begin
timeout( timeout ){
TCPSocket.open( host, service){}
}
rescue Exception => ex
# handle any expected exceptions
case ex
when Errno::ECONNREFUSED
return false
when Timeout::Error
return false
else
# pass the buck
raise ex
end
end
true
end

Doing a ping by connecting is rather heavyweight though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top