# Require functionality

**URL:** <https://www.ruby-forum.com/t/require-functionality/120759>\
**Category:** Ruby\
**Created:** [November 20, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/require-functionality/120759 "2007-11-20T12:41:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 20, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/require-functionality/120759/1 "2007-11-20T12:41:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 20, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/require-functionality/120759/2 "2007-11-20T12:56:04Z")

</div>

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](http://ruby.about.com/od/learnruby/ss/require_load_2.htm)

Csmr

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 20, 2007, 12:58pm UTC](https://www.ruby-forum.com/t/require-functionality/120759/3 "2007-11-20T12:58:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 20, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/require-functionality/120759/4 "2007-11-20T16:00:55Z")

</div>

On Nov 20, 5:36 am, Junkone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
