# Practices

**URL:** <https://www.ruby-forum.com/t/practices/94044>\
**Category:** Ruby\
**Created:** [April 19, 2006, 2:20am UTC](https://www.ruby-forum.com/t/practices/94044 "2006-04-19T02:20:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 19, 2006, 2:20am UTC](https://www.ruby-forum.com/t/practices/94044/1 "2006-04-19T02:20:38Z")

</div>

Is there anything of a best practices on how to use your own modules?

I’ve been doing a LOT of this in perl and have just started to play with  
it in Ruby.

Some things I am transistioning from are:  
use lib – how do I specify an alternate location for files?

modules/object/files:  
It may be bad form to put more than on class in a single file, I know it  
is in  
Perl. But is there a reason not to? I have some classes that are very  
tightly  
related in inheritance and they are very small. I don’t see much  
advantage to  
ever physically breaking them apart.

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 19, 2006, 9:32am UTC](https://www.ruby-forum.com/t/practices/94044/2 "2006-04-19T09:32:49Z")

</div>

I have been wondering the same. By looking at libraries out there, it  
looks like there isn’t a standard view about this.

Does anybody have a pointer to conventions to structure  
directories/files/modules  
and classes?

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 19, 2006, 11:11am UTC](https://www.ruby-forum.com/t/practices/94044/3 "2006-04-19T11:11:57Z")

</div>

2006/4/19, Tom A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Is there anything of a best practices on how to use your own modules?
> 
> I’ve been doing a LOT of this in perl and have just started to play with it in Ruby.
> 
> Some things I am transistioning from are:  
> use lib – how do I specify an alternate location for files?

There is an env var, I believe it’s RUBY\_LIB.

> modules/object/files:  
> It may be bad form to put more than on class in a single file, I know it is in  
> Perl. But is there a reason not to? I have some classes that are very tightly  
> related in inheritance and they are very small. I don’t see much advantage to  
> ever physically breaking them apart.

Then don’t do it. It’s fine to have them in the same file.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 19, 2006, 11:45am UTC](https://www.ruby-forum.com/t/practices/94044/4 "2006-04-19T11:45:59Z")

</div>

Jonas P. wrote:

> Le 19 avr. 06 à 11:09, Robert K. a écrit :
> 
> > There is an env var, I believe it’s RUBY\_LIB.
> 
> It is RUBYLIB

Thank you. I found more information on it.  
Seems that it will work but it has limitations, like safe mode.

If I have a safemode \> 0 and files in ~/lib/ how would I use them?  
I can’t edit the system environment in this case.  
Would I convert the ~/ to the fullpath and use require:

require ‘/home/username/lib/noclass’

?

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 19, 2006, 11:18am UTC](https://www.ruby-forum.com/t/practices/94044/5 "2006-04-19T11:18:12Z")

</div>

Le 19 avr. 06 à 11:09, Robert K. a écrit :

> There is an env var, I believe it’s RUBY\_LIB.

It is RUBYLIB

Cheers,  
zimbatm

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 19, 2006, 3:17pm UTC](https://www.ruby-forum.com/t/practices/94044/6 "2006-04-19T15:17:16Z")

</div>

On 4/19/06, Tom A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If I have a safemode \> 0 and files in ~/lib/ how would I use them?  
> I can’t edit the system environment in this case.  
> Would I convert the ~/ to the fullpath and use require:
> 
> require ‘/home/username/lib/noclass’
> 
> ?

I think a fairly common idiom in cases like this (one I have used  
myself) is to add to the LOAD\_PATH within your script (at the top  
before your requires):

$LOAD\_PATH.unshift ‘~/lib’

Then require will check in ~/lib for scripts before checking the other  
default locations.

Ryan

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 19, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/practices/94044/7 "2006-04-19T17:00:06Z")

</div>

On 4/18/06, Tom A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> modules/object/files:  
> It may be bad form to put more than on class in a single file, I know it is in  
> Perl. But is there a reason not to? I have some classes that are very tightly  
> related in inheritance and they are very small. I don’t see much advantage to  
> ever physically breaking them apart.

Will you ever load them without each other? If so, then split them. If  
not, don’t.

-austin

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 19, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/practices/94044/8 "2006-04-19T18:20:54Z")

</div>

If the safe mode is a problem, do all your require’ing before changing  
the safe mode (I think). You might also have an unsafe program that  
does the require’ing, then includes and runs the safe part.

For non-trivial programs, I have begun using a structure as follows. I  
picked up the bin/ doc/ test/ somewhere (thanks whomever you are), but  
can’t remember where, and added to it.

labeler/  
bin/ # “Binaries,” i.e. files that are run directly  
lbl.rb  
config/ # config files.  
config.yaml  
label\_defs.yaml  
data/ # Any data needed to setup/run the system  
doc/ # RDoc and other documentation  
classes  
files  
…  
email-from-blah-blah.txt  
some-ref.pdf  
lib/ # rb’s go here  
entity.rb  
label.rb  
…  
release/ # Files needed to build a release  
build.rb # of the system go here.  
labeler-1.1.5.zip # zip created to send customer  
# \*.zip is ignored by svn here.  
test/ # unit tests go here  
data/ # it would be better if my test data  
# were in a dir like this. currently  
# it’s all just glommed into test/.  
tc\_lbl.rb # tc\_ = test case  
test\_labeler.rb  
…  
ts\_labeler.rb # a test suite  
vendor/  
extensions/ # my own extensions to Ruby core classes  
integer\_ext.rb # adds #is\_odd?, #is\_even?  
regexp\_ext.rb # adds #+ method.  
serial/ # a serial library  
vendor.rb # see below

In my main program, bin/lbl.rb, I have the following:

# Add …/lib and …/vendor to the load path.

$:.unshift File.expand\_path(File.join(File.dirname( **FILE** ), “…”,  
“lib”))  
$:.unshift File.expand\_path(File.join(File.dirname( **FILE** ), “…”,  
“vendor”))  
require ‘vendor’  
require ‘optparse’  
…  
require ‘label\_printer’  
…

# Location of the config file.

CONFIG\_FILE = File.join(File.dirname( **FILE** ), “…”, “config”,  
“config.yaml”)  
…  
config = YAML.load\_file(filename) # load the config data  
…  
####################

vendor/vendor.rb is the following:

#!/usr/bin/env ruby

# 

# vendor

# 

# Created by John J. on 2006-03-31.

# Copyright © 2006 John J. Software, LLC, All rights reserved.

# 

# Prepend all vendor subdirectories to the load path.

# 

dir=Dir.new(File.dirname( **FILE** ))  
dir = dir.to\_a  
not\_dots = dir.delete\_if { |d| d =~ /^./ }  
not\_files = not\_dots.delete\_if { |d|  
to\_check = File.join(File.dirname( **FILE** ), d)  
File.file?(to\_check)  
}  
not\_files.each { |a\_dir|  
to\_add = File.join(File.dirname( **FILE** ), a\_dir)  
to\_add = File.expand\_path(to\_add)  
$:.unshift to\_add  
}

Which prepends all subdirectories of vendor onto the load path.  
###################

I added an option to the main program (–test) that will run all the  
unit tests:

opts = OptionParser.new  
…  
opts.on("–test", “Run unit tests on this program.”) {  
Dir.chdir(‘test’)  
system(‘ruby ts\_labeler.rb’)  
exit(1)  
}

This is so the customer (or I) can do:  
lbl --test  
at my direction to test the system.

################

Since I deploy this system on Windows, I have the obligatory lbl.bat  
file in the system’s root:  
@echo off

rem Use OLD\_PATH to keep paths found from accumulating in PATH.  
SET OLD\_PATH=%PATH%

rem Prefered path is last.  
IF EXIST c:\djgpp\bin\ruby.exe SET PATH=c:\djgpp\bin;%OLD\_PATH%  
IF EXIST c:\usr\local\bin\ruby.exe SET PATH=c:\usr\local\bin;%OLD\_PATH%  
IF EXIST c:\ruby\bin\ruby.exe SET PATH=c:\ruby\bin;%OLD\_PATH%

ruby bin/%0.rb %\*  
###################

All the above (files, dirs, data, etc.) is kept in an SVN repository.  
Everything in the vendor folder is external, meaning they are housed  
elsewhere, and brought into vendor on this system and others I write.

For small systems or utilities for my own use, with a couple of files,  
I just put everything in one directory.

As far as dividing classes into files, go with your gut. I put like  
things together. There is no need in having thirty’leven (colloquialism  
used in the Southeastern United States meaning ‘a lot of’, see also  
‘dollar two ninety eight’) files just so each class is in it’s own  
file. Writing the unit tests seems to help with deciding how to group  
things too. Unit testing also helps you divide functions and methods  
into manageable chunks.

I highly recommend using Subversion and unit tests (if you couldn’t  
tell). There is a learning curve, but both are indispensable.

Hope this helps!

Regards,  
JJ

On 18-Apr-2006, at 20:17, Tom A. wrote:

> it is in Perl. But is there a reason not to? I have some classes  
> that are very tightly related in inheritance and they are very small.  
> I don’t see much advantage to ever physically breaking them apart.

* * *

Help everyone. If you can’t do that, then at least be nice.

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 26, 2006, 12:20am UTC](https://www.ruby-forum.com/t/practices/94044/9 "2006-04-26T00:20:49Z")

</div>

Spectacular! Thanks.
