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?

···

--
Garance Alistair Drosehn = drosihn@gmail.com
Senior Systems Programmer
Rensselaer Polytechnic Institute; Troy, NY; USA

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?

begin
   require 'foo'
rescue LoadError
   puts "You need foo for this!"
end

···

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

--
Garance Alistair Drosehn = drosihn@gmail.com
Senior Systems Programmer
Rensselaer Polytechnic Institute; Troy, NY; USA

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.

···

--
Posted via http://www.ruby-forum.com/.

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.

···

On 8/31/06, Garance A Drosehn <drosihn@gmail.com> 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
    $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?

--
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 <rick.denatale@gmail.com> ...
On 8/31/06, Eero Saynatkari <eero.saynatkari@kolumbus.fi> ...

···

On 8/31/06, Logan Capaldo <logancapaldo@gmail.com> ... ... all wrote

You gotta rescue LoadError

--
Rick DeNatale

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

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:

···

On 8/31/06, Rick DeNatale <rick.denatale@gmail.com> wrote:

Ah, answers in three-part harmony.

On 8/31/06, Rick DeNatale <rick.denatale@gmail.com> ...
On 8/31/06, Eero Saynatkari <eero.saynatkari@kolumbus.fi> ...
On 8/31/06, Logan Capaldo <logancapaldo@gmail.com> ... > > ... all wrote

> You gotta rescue LoadError

--
Garance Alistair Drosehn = drosihn@gmail.com
Senior Systems Programmer
Rensselaer Polytechnic Institute; Troy, NY; USA