On Apr 16, 2008, at 10:37 AM, Joel VanderWerf wrote:
> Eric Hodel wrote:
>> On Apr 16, 2008, at 00:50 AM, Christian Johansen wrote:
>>> Joel VanderWerf wrote:
>>>> Eric Hodel wrote:
>>>>> The simplest way is `ruby -Ilib bin/myapp` from the root of the  
>>>>> project
>>>>> dir.
>>>>>
>>>>> (-I directories take precedence over RUBYLIB directories.)
>>>>
>>>> Also, it's fragile with respect to the cwd. You have to be in  
>>>> myapp/ to
>>>> run it, and also the program can't Dir.chdir without breaking the
>>>> reference to the lib dir.
>>>
>>> Exactly, and this was my original problem as well.I think I'll go  
>>> with
>>> Eric Hodels solution for now. Also, I will put the code behind the
>>> binary in myapp/lib/myapp/command.rb
>> My solution works if ordinarily you distribute myapp as a gem as  
>> you avoid the problems with it that Joel pointed out.
>
> Eric, how does that work? Does gem install put a stub in your bindir  
> that calls ruby with the -I? Something else?
>
> Looking at what gems did with rake, there is a stub in /usr/local/ 
> bin, but there doesn't seem to be anything that modifies RUBYLIB or  
> uses the -I switch. Maybe there's some magic in the line "gem  
> 'rake', version"?

Kernel#gem activates rake (adds its load_paths to $LOAD_PATH) so that  
Kernel#load can find the executable.  (See `ri Kernel#gem` and `ri  
Gem::activate` for the full description.)  RubyGems only hooks into  
Kernel#require, so rake needs to be activated for load to work.

> Is there somewhere in the gem docs that addresses how to use it for  
> an application specifically?

(I think you mean "gem" by "it", sorry if I'm confused.)  For  
Kernel#gem you only need to use Kernel#gem in two situations:

a) you want a specific version of a gem
b) the gem you're using replaces ruby standard library functionality  
(soap4r, rdoc and rake are the only three I know about).