tags:

views:

1524

answers:

2

I'm after an overview/clarification of the ideal project structure for a ruby (non-rails/merb/etc) project. I'm guessing it follows along the lines of:

app/
  bin/                  #Files for command-line execution
  lib/
    appname.rb
    appname/            #Classes and so on
  Rakefile              #Running tests
  README
  test,spec,features/   #Whichever means of testing you go for
  appname.gemspec       #If it's a gem

Have I got something wrong? What parts have I missed out?

+17  A: 

I think that is pretty much spot on. By default, Rubygems will add the lib directory to the loadpath, but you can push any directory you want onto that using the $: variable. i.e.

$:.push File.expand_path(File.dirname(__FILE__) + '/../surfcompstuff')

That means when you have say, surfer.rb in that dir, you can require "surfer" anywhere and the file will be found.

Also, as a convention, classes and singletons get a file and modules get a directory. For instance, if you had the LolCatz module and the LolCatz::Moar class that would look like:

lib/
  appname.rb
  lolcatz/
    moar.rb

That is why there is an lib/appname folder because most libraries are in the appname namespace.

Additionally, if you try running the command newgem --simple [projectname] that'll quickly generate a scaffold for you with just the bare essentials for a Ruby project (and by extension a Ruby Gem). There are other tools which do this, I know, but newgem is pretty common. I usually get rid of the TODO file and all the script stuff.

Chris Lloyd
Note that you'll have to [sudo] gem install newgem to get the newgem command...
Mike Woodhouse
Sweet, thanks Chris
dylanfm
sweet. I didn't know about newgem. My non-rails projects have often mirrored the Rails structure because I've found it familiar. Thanks for this best-practice tip.
berlin.ab
Another important file might be "LICENSE"
bluehavana
I don't get the behavior you describe with `lib` being automatically added to the loadpath. Is it a 1.9 thing? Any special configuration required to get it to happen?
Emily
+3  A: 

I attempt to mimic the Rails project structure because my team, which usually deals with Rails, will understand the structure better than another configuration. Convention over Configuration - bleeding over from Rails.

berlin.ab