require functionality

J

Junkone

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

Xavier Noria

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

yermej

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
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top