[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rescuing a 'require'

Garance A Drosehn

9/1/2006 2:40:00 AM

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

5 Answers

Logan Capaldo

9/1/2006 3:11:00 AM

0


On Aug 31, 2006, at 10:39 PM, 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
> $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


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


e

9/1/2006 3:15:00 AM

0

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-....

Rick DeNatale

9/1/2006 3:16:00 AM

0

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?

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.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Rick DeNatale

9/1/2006 3:46:00 AM

0

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.denh...

Garance A Drosehn

9/2/2006 8:40:00 PM

0

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

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! :-)

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