[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

require functionality

Junkone

11/20/2007 11:37:00 AM

hello
can i verify if a library is loaded before doing a require 'library'.
if so how.
thanks in advance
3 Answers

Casimir

11/20/2007 11:51:00 AM

0

Junkone kirjoitti:
> hello
> can i verify if a library is loaded before doing a require 'library'.
> if so how.
> thanks in advance
As far as I know the very nature of 'require' is such that if it fails
to include the required file or library, the whole program will halt.

The predefined variable $" contains the module names loaded by require.

See http://ruby.about.com/od/learnruby/ss/require_...

Csmr

Xavier Noria

11/20/2007 11:56:00 AM

0

On Nov 20, 2007, at 12:39 PM, Junkone wrote:

> hello
> can i verify if a library is loaded before doing a require 'library'.
> if so how.
> thanks in advance

require loads a library only once, it returns a boolean that indicates
whether it actually loaded the file[*].

The files loaded so far by require are stored in the array $", so you
could check that if you really need it[**].

-- fxn

[*] In Rails require is redefined and returns a different thing.

[**] Actual file names as passed or resolved by require are stored, so
strictly speaking you have _paths_ and they are not normalized,
expanded, whatever. Thus, the same "library" may have been loaded
twice if the paths to the .rb were different. See footnote on page 117
of the Pickaxe.



yermej

11/20/2007 2:59:00 PM

0

On Nov 20, 5:36 am, Junkone <junko...@gmail.com> wrote:
> hello
> can i verify if a library is loaded before doing a require 'library'.
> if so how.
> thanks in advance

By "if a library is loaded", do you mean if a library exists on the
system?

If that's what you're looking for, it's probably easiest to wrap the
require in a begin/rescue/end, catch the LoadError which is raised if
the library doesn't exist, and then do what needs to be done.

begin
require "optional_library"
rescue LoadError
puts "couldn't find optional_library, continuing without it"
end