Rescuing a 'require'

  • Thread starter Garance A Drosehn
  • Start date
G

Garance A Drosehn

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?
 
L

Logan Capaldo

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

 
E

Eero Saynatkari

Garance said:
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.
 
R

Rick DeNatale

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?

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/
 

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
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top