Forum: Ruby require an entire package?

Posted by Juston (Guest)
on 2009-07-02 00:21
(Received via mailing list)
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!
Posted by Michael Linfield (macgoober)
on 2009-07-02 00:26
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
Posted by Michael Linfield (macgoober)
on 2009-07-02 00:35
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
Posted by Juston (Guest)
on 2009-07-02 00:58
(Received via mailing list)
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!
Posted by Gary Chris (rlzyoner)
on 2009-07-02 01:52
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.
Posted by David A. Black (Guest)
on 2009-07-02 01:57
(Received via mailing list)
Hi --

On Thu, 2 Jul 2009, Gary Chris 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.