Require functionality

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

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

Csmr

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.

···

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

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

···

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