# Big project in ruby

**URL:** <https://www.ruby-forum.com/t/big-project-in-ruby/82530>\
**Category:** Ruby\
**Created:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/big-project-in-ruby/82530 "2007-01-19T16:31:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![miquel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@miquel](https://www.ruby-forum.com/u/miquel)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/big-project-in-ruby/82530/1 "2007-01-19T16:31:09Z")

</div>

Hi all

I’m trying to design a big application in ruby and I have (among  
others) a doubt/question.

I’m creating folders with the classes (I supose influenced by java  
package distribution structure) and I want to use them.

So, when I write into a .rb file require ‘whatever’ I’m using the form  
require ‘folder\_a/folder\_b/class\_file.rb’. There any ruby  
variable which I can write require ruby\_var+“/class\_file.rb”?

Thanks in advance

Kind regards

Miquel

* * *

LLama Gratis a cualquier PC del Mundo.  
Llamadas a fijos y móviles desde 1 céntimo por minuto.  
[http://es.voice.yahoo.com](http://es.voice.yahoo.com)

---

<div class="post-metadata">

**Author:** ![miquel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@miquel](https://www.ruby-forum.com/u/miquel)\
**Post date:** [September 25, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/big-project-in-ruby/82530/2 "2007-09-25T22:32:31Z")

</div>

On 1/11/07, ChrisH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> They could also add the path to the $LOAD\_PATH, i.e:
> 
> $LOAD\_PATH\<\<'‘folder\_a/folder\_b’  
> require ‘class\_file’

This can create a problem if you have two files with the same name in  
different directories.

I would recommend designing a fixed folder structure for your library,  
rooted in one directory, such as:

lib  
package1  
class1.rb  
class2.rb  
package2  
classA.rb  
classB.rb

Each class that might depend on other classes makes a require  
statement as if it was rooted in the lib directory. E.g. in  
package1\class2.rb, have:

require ‘class1’ # Current directory always on load path  
require ‘package2\classA’ # File in another directory requires path

To make that work, one file needs to be loaded before others which  
adds the full path to the "lib" directory to the $LOAD\_PATH variable  
(AKA known as $:).

I recommend creating a small file that just has requires, which is  
used to load your library from a given script. Example, imagine your  
library lives under an app directory:

app  
main.rb  
library.rb  
lib  
package1  
package2  
etc.

library.rb would have these contents:

$: \<\< “path to lib"  
require 'package1\class1” # Load initial library files. Not  
absolutely necessary.  
require 'package2\classA" # Load initial library files. Not  
absolutely necessary.

Then in main.rb, which is your app, you can just have this statement  
to get your library loaded:

require ‘library’

Alternatively, you can make the modifications to $: in your main.rb  
file, and then load the library. This skips the step of creating a  
“library.rb” file, so main.rb just looks like:

$: \<\< “path to lib"  
require 'package1\class1”  
require 'package2\classA"

Hope that helps.

Justin

---

<div class="post-metadata">

**Author:** ![miquel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@miquel](https://www.ruby-forum.com/u/miquel)\
**Post date:** [September 25, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/big-project-in-ruby/82530/3 "2007-09-25T22:41:54Z")

</div>

On 11.01.2007 18:18, Miquel wrote:

> So, when I write into a .rb file require ‘whatever’ I’m using the form  
> require ‘folder\_a/folder\_b/class\_file.rb’. There any ruby

You only need

require ‘folder\_a/folder\_b/class\_file’

> variable which I can write require ruby\_var+"/class\_file.rb"?

You can use any variable there since require is a function call like any  
other - at least from the caller’s perspective

ruby\_var = ‘folder\_a/folder\_b’  
require ruby\_var + “/class\_file”

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![miquel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@miquel](https://www.ruby-forum.com/u/miquel)\
**Post date:** [September 25, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/big-project-in-ruby/82530/4 "2007-09-25T22:52:18Z")

</div>

Robert K. wrote:

> On 11.01.2007 18:18, Miquel wrote:
> 
> > So, when I write into a .rb file require ‘whatever’ I’m using the form  
> > require ‘folder\_a/folder\_b/class\_file.rb’. There any ruby  
> > variable which I can write require ruby\_var+"/class\_file.rb"?
> 
> You can use any variable there since require is a function call like any  
> other - at least from the caller’s perspective
> 
> ruby\_var = ‘folder\_a/folder\_b’  
> require ruby\_var + “/class\_file”

They could also add the path to the $LOAD\_PATH, i.e:

$LOAD\_PATH\<\<’‘folder\_a/folder\_b’  
require ‘class\_file’

cheers  
Chris
