Rescuing a 'require'

I wanted to use Socket.gethostname to get the current hostname
without executing a unix command. So:

require ‘socket’
$this_host = Socket.gethostname

I have to run this code on several platforms, and it turned out that
some of them do not have ‘socket’. So, my first idea was change
it to:

begin
require ‘socket’
$this_host = Socket.gethostname
rescue
$this_host = hostname.chomp
end

but the require still caused the script to terminate on the platforms
which didn’t have ‘socket’. I already have an alternate solution for
this specific case, but I’m wondering if there was something that I
missed. Is there any way to catch errors from a require command?

On Aug 31, 2006, at 10:39 PM, Garance A Drosehn wrote:

begin

begin
require ‘foo’
rescue LoadError
puts “You need foo for this!”
end

Garance A Drosehn wrote:

I wanted to use Socket.gethostname to get the current hostname
without executing a unix command. So:

require ‘socket’
$this_host = Socket.gethostname

I have to run this code on several platforms, and it turned out that
some of them do not have ‘socket’. So, my first idea was change
it to:

begin
require ‘socket’
$this_host = Socket.gethostname
rescue

rescue LoadError

$this_host = `hostname`.chomp

end

but the require still caused the script to terminate on the platforms
which didn’t have ‘socket’. I already have an alternate solution for
this specific case, but I’m wondering if there was something that I
missed. Is there any way to catch errors from a require command?

You need to give the exception class; rescue by itself
only catches subclasses of StandardError.

On 8/31/06, Garance A Drosehn [email protected] wrote:

begin
require ‘socket’
$this_host = Socket.gethostname
rescue
$this_host = hostname.chomp
end

but the require still caused the script to terminate on the platforms
which didn’t have ‘socket’. I already have an alternate solution for
this specific case, but I’m wondering if there was something that I
missed. Is there any way to catch errors from a require command?

irb(main):006:0> begin
irb(main):007:1* require ‘foo’
irb(main):008:1> rescue LoadError
irb(main):009:1> puts “caught it”
irb(main):010:1> end
caught it
=> nil

rescue without a specific exception or exceptions only catches
subclasses of StandardError. LoadError is a subclass of ScriptError
which in turn is a subclass of Exception.

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/

Ah, answers in three-part harmony.

On 8/31/06, Rick DeNatale [email protected]
On 8/31/06, Eero S. [email protected]
On 8/31/06, Logan C. [email protected]

… all wrote

You gotta rescue LoadError


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On 8/31/06, Rick DeNatale [email protected] wrote:

Ah, answers in three-part harmony.

On 8/31/06, Rick DeNatale [email protected]
On 8/31/06, Eero S. [email protected]
On 8/31/06, Logan C. [email protected]

… all wrote

You gotta rescue LoadError

Ah. Very good. I figured it was something simple that I was missing.
It makes a lot of sense now. It’s nice to receive answers in such
harmony! :slight_smile: