require an entire package?

J

Juston

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.*' 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!
 
M

Michael Linfield

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
 
M

Michael Linfield

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
 
J

Juston

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!
 
G

Gary Chris

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

David A. Black

Hi --

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

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top