# Require an entire package?

**URL:** <https://www.ruby-forum.com/t/require-an-entire-package/171411>\
**Category:** Ruby\
**Created:** [July 2, 2009, 12:21am UTC](https://www.ruby-forum.com/t/require-an-entire-package/171411 "2009-07-02T00:21:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Juston](https://www.ruby-forum.com/u/Juston)\
**Post date:** [July 2, 2009, 12:21am UTC](https://www.ruby-forum.com/t/require-an-entire-package/171411/1 "2009-07-02T00:21:22Z")

</div>

I’m about three weeks into Ruby coming from C++ and Java and enjoying  
it so far. I’ve spent a lot of time reading “The Ruby Way” 2nd ed from  
about cover to cover but the old brain is still very much in the  
beginner’s stage of understanding some of subtleties

The problem I’m trying to solve is that I am writing a rather large  
application that consists of about 8 packages each with their own set  
of Ruby classes. It kind of looks like this:

Project

```
Common
    -Main.rb
    -Designator.rb
Interpreter
    -DomainInterpreter.rb

```

…(more packages with more classes/modules)

In most instances I have to require one or more item from the same (or  
other) package for each given class to do its job. However, in some  
cases a class requires an entire package. While it isn’t a problem to  
require each one individually, it is a bit of a nuisance. In java you  
can do ‘import [java.io](http://java.io).\*’ and import a whole set of items at once.

The question I have is, “Is there a way to require multiple items from  
the same package in one line of code in Ruby 1.8.6?”

Thanks in advance for the time and help!

---

<div class="post-metadata">

**Author:** ![Juston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Juston](https://www.ruby-forum.com/u/Juston)\
**Post date:** [July 2, 2009, 12:26am UTC](https://www.ruby-forum.com/t/require-an-entire-package/171411/2 "2009-07-02T00:26:34Z")

</div>

You could just require ‘filename.rb’

This would pick up all class and subclass functions located in that  
file.

Global variables are your friend 🙂

- Mac

---

<div class="post-metadata">

**Author:** ![Juston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Juston](https://www.ruby-forum.com/u/Juston)\
**Post date:** [July 2, 2009, 12:35am UTC](https://www.ruby-forum.com/t/require-an-entire-package/171411/3 "2009-07-02T00:35:13Z")

</div>

Woops, I may have misunderstood, if you meant require everything in say  
the ‘Common’ directory/package… you could do something such as:

Dir.glob("\*.rb").each {|x| require x}

given the fact that you would need to be in the Common directory

- Mac

---

<div class="post-metadata">

**Author:** ![Juston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Juston](https://www.ruby-forum.com/u/Juston)\
**Post date:** [July 2, 2009, 1:52am UTC](https://www.ruby-forum.com/t/require-an-entire-package/171411/4 "2009-07-02T01:52:50Z")

</div>

Hello,

I just replied to you over on ubuntuforums but i’ll stick it here also.

You could just add the directory containing the files to the load\_path  
array

$LOAD\_PATH \<\< “#{File.dirname( **FILE** )}/…/…/dir\_containing\_files”

This will load all files that are located within the directory.

As I said over on ubuntuforums, this may be overkill.

---

<div class="post-metadata">

**Author:** ![Juston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Juston](https://www.ruby-forum.com/u/Juston)\
**Post date:** [July 2, 2009, 12:58am UTC](https://www.ruby-forum.com/t/require-an-entire-package/171411/5 "2009-07-02T00:58:45Z")

</div>

I had tried two or three things really similar but couldn’t quite get  
it right  
#Dir.each("…/Modifier") { |mod| require mod}

I wound up using a modified version of your suggestion:  
Dir.glob("…/Modifier/_/_.rb").each {|modifier| require modifier}

This will save me a lot of time and maintenance, thanks!

---

<div class="post-metadata">

**Author:** ![Juston](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@Juston](https://www.ruby-forum.com/u/Juston)\
**Post date:** [July 2, 2009, 1:57am UTC](https://www.ruby-forum.com/t/require-an-entire-package/171411/6 "2009-07-02T01:57:46Z")

</div>

Hi –

On Thu, 2 Jul 2009, Gary C. wrote:

> Hello,
> 
> I just replied to you over on ubuntuforums but i’ll stick it here also.
> 
> You could just add the directory containing the files to the load\_path  
> array
> 
> $LOAD\_PATH \<\< “#{File.dirname( **FILE** )}/…/…/dir\_containing\_files”
> 
> This will load all files that are located within the directory.

Adding directories to the load path doesn’t load any files. The load  
path is just where Ruby will search when you try to load something.  
You still have to do it explicitly.

David
