From: Rick DeNatale Date: 2006-09-01T12:16:02+09:00 Subject: Re: Rescuing a 'require' On 8/31/06, 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? 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/