On Apr 14, 8:15 am, Christian Johansen <chrisj... / gmail.com> wrote:
> I'm writing my first Ruby command line application (beyond a simple
> script) and I'm little unsure about how to structure the application and
> the best use of require. I've come as far as to have the following file
> structure:
>
> myapp/
>   bin/
>     myapp
>   docs/ (RDOC files)
>   lib/
>     myapp.rb
>     myapp/
>       files and folders here
>   test/ (Test::Unit tests)

I would suggest:

  myapp/
    bin/
      myapp
    doc/
      rdoc/ (RDOC files)
    lib/
      myapp.rb
      myapp/
        files and folders here
    test/ (Test::Unit tests)

> And the application is working, but there are a few things of which I am
> unsure:
>
> The bin/myapp file is a symlink to the lib/myapp.rb file which has a
> shebang line. This doesn't really work, because inside lib/myapp.rb I
> have:

Don't use a symlink. Make something like lib/myapp/command.rb, then in
the bin:

  require 'myapp/command'

  MyApp::Command.start  # or what have you

> which doesn't make sense when running the application from anywhere else
> than myapp/lib
>
> I also tried
>
> $MYAPP_ROOT = File.expand_path(File.join(File.dirname(__FILE__),
> 'myapp'))
> $LOAD_PATH << File.expand_path($MYAPP_ROOT)
>
> Which still doesn't work because this also relies on cwd...
>
> How do I work around this? What's the best practice way of making these
> things make sense? Do you use the $RUBYLIB variable to set the full path
> to the application? I was hoping to create the application as standalone
> as possible, so installation will be easy.

To run your app you will need to first go through an install phase.
There a few ways to do this. The oldest is to add setup.rb to you
project and run that between trying your app out. Another is to
generate a .gem and install that between trial runs (you can automate
this process for convenience). In my case, I use Rolls, but it's still
a little rough around the edges so I'm not actively recommending it to
anyone yet. But you can contact me off list if you'd like to do so.

On the other hand, if you do TDD you don't really need go through an
install phase often. Your test runner should add lib/ to the
$LOAD_PATH. So you can run your tests without ever installing your
app.

HTH,
T.