« back

Cutting a Gem- Directories and expanding your path

27 Aug 2012

A couple of weeks ago, I made my first Ruby gem. I did this in preparation for converting my command line Tic Tac Toe(TTT) app into a gem for web consumption. I followed this tutorial. It went pretty smoothly.

After finishing my TTT app, I went to turn it into a gem using the tutorial I used before as a guide. I quickly found that with more files and folders, the tutorial is not quite straight forward. I then went looking for some other tutorials and a theme began to emerge. All of the tutorials were walking you through building a gem from scratch. Also, the tutorials really had no more than two files they were dealing with.

Being my first gem, I wasn't really sure about the way the files and folders should be set up, the way the files in the app or in the gemspec should be required, or what the new path should look like. Since it is traditional to set your directory structure up a particular way to prevent naming issues, I wanted to make sure I was doing this properly. Here is what I found...

When working with your directory structure for a gem, it is advised to set it up like this...

 1 $ tree -d mygem
 2 |-- lib/
 3 |   |-- mygem.rb
 4 |   `-- mygem/
 5 |       |-- base.rb
 6 |       `-- version.rb
 7 |-- test/
 8 |-- bin/
 9 |-- Rakefile
10 `-- mygem.gemspec

Here is what mine looks like...

 1 $ tree -d tic_tac_toe_angeleah
 2 |-- lib/
 3 |   |-- tic_tac_toe_angeleah.rb
 4 |   `-- tic_tac_toe_angeleah/
 5 |       |
 6 |       `-- ttt/
 7 |           |--all the ttt files.rb
 8 |-- spec/
 9 |-- bin/
10 `-- tic_tac_toe_angeleah.gemspec

Doing that threw off my path. I was originally using my bin file to kick everything off in my command line version and my path looked like this...

1     $: << File.expand_path(File.dirname(__FILE__) + '/../lib/')

Since I would be placing my path in my ang_ttt_gem.rb file I needed to augment the path to this...

1     $: << File.expand_path(File.dirname(__FILE__) + '/ang_ttt_gem/')
comments powered by Disqus