# Strategies for writing Ruby applications to be easily distri

**URL:** <https://www.ruby-forum.com/t/strategies-for-writing-ruby-applications-to-be-easily-distri/79976>\
**Category:** Ruby\
**Created:** [December 20, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/strategies-for-writing-ruby-applications-to-be-easily-distri/79976 "2006-12-20T19:24:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zev\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb8c5e/32.png) [@Zev\_B](https://www.ruby-forum.com/u/Zev_B)\
**Post date:** [December 20, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/strategies-for-writing-ruby-applications-to-be-easily-distri/79976/1 "2006-12-20T19:24:53Z")

</div>

Hello,

This is mostly a repost of ruby-talk[229723] with some clarifications  
and an appeal for ideas.

I have recently attempted to write a RubyGem for the Saikuro project.  
Unfortunately, I have learned that there is a mismatch between how I  
wrote Saikuro and how RubyGems expects executables to work.

I would like to know is are there any better or commonly followed ways  
of writing executables that work with RubyGems and other installation  
programs such as setup.rb ?

Below are the differences between how I wrote Saikuro and what  
RubyGems does.

1. 

Saikuro uses the “if **FILE** == $0” check before doing the  
“application work”. When RubyGems creates a new gemified saikuro  
wrapper to call the real saikuro bin “$0” is the gemified saikuro and  
not the real one. Thus, by default nothing happens.

For fun I cheated and commented out the “if **FILE** ” check and  
replaced it with “if true”. Saikuro will work with this change, but I  
then ran “saikuro -?” to get a usage message and found the next  
problem:

1. 

I was reading through the Programming Ruby book and found a part that  
showed how to use rdoc to create usage messages. I figured this might  
be better than my normal technique so I used it in Saikuro. This has  
caused a number of problems actually.

- With the above cheat I get the gem message:  
–  
/usr/bin/saikuro: invalid option – ?  
This file was generated by RubyGems.

## The application ‘Saikuro’ is installed as part of a gem, and this file is here to facilitate running it.

Which is not the usage message I wanted.

My original goal was to keep Saikuro simple so that as a single file  
nothing complicated was needed. But now I suppose that should change.

I am planning to move all logic above and “if **FILE** == $0” block  
into a separate library file. Then add a require to this newly made  
file and remove the “if **FILE** ” check logic to make the insides of  
the block automatically run.

Next I will stop using rdoc usage and simply put the usage message in  
a method that outputs a String.

Does this sound like a good strategy?

Thanks,  
Zev

---

<div class="post-metadata">

**Author:** ![Zev\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/eb8c5e/32.png) [@Zev\_B](https://www.ruby-forum.com/u/Zev_B)\
**Post date:** [December 20, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/strategies-for-writing-ruby-applications-to-be-easily-distri/79976/2 "2006-12-20T19:29:04Z")

</div>

On Dec 18, 2006, at 22:34, Zev B. wrote:

> This is mostly a repost of ruby-talk[229723] with some clarifications  
> and an appeal for ideas.
> 
> I have recently attempted to write a RubyGem for the Saikuro project.  
> Unfortunately, I have learned that there is a mismatch between how I  
> wrote Saikuro and how RubyGems expects executables to work.
> 
> I would like to know is are there any better or commonly followed ways  
> of writing executables that work with RubyGems and other installation  
> programs such as setup.rb ?

My executables typically consist of three lines:

#!/usr/local/bin/ruby -w  
require ‘some\_file’  
SomeFile.run

Where SomeFile::run is something like:

class SomeFile  
def self.run(args = ARGV)  
options = process\_args args  
new(option[:foo]).run  
end  
end

> 1. 
> 
> I was reading through the Programming Ruby book and found a part that  
> showed how to use rdoc to create usage messages. I figured this might  
> be better than my normal technique so I used it in Saikuro. This has  
> caused a number of problems actually.

What does this technique look like? I’m not familiar with it.

> My original goal was to keep Saikuro simple so that as a single file  
> nothing complicated was needed. But now I suppose that should change.
> 
> I am planning to move all logic above and “if **FILE** == $0” block  
> into a separate library file. Then add a require to this newly made  
> file and remove the “if **FILE** ” check logic to make the insides of  
> the block automatically run.

I keep these together, but put the logic that would normally go in  
“if **FILE** == $0” into its own method or methods. That keeps the  
executable as simple as possible.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!
