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
>