Building Ruby Gems
Ruby has packages called gems which are easy to distribute and can be used when building a ruby app. RSpec the testing framework, for example, is a gem that I use in every ruby project I build.
On Friday I took my Tic Tac Toe game and extracted the code into three gems. My mentors had told me that it wasn’t that difficult, but it wasn’t until I built my first one in under an hour that I realised how straightforward it actually is.
I used the Bundler docs which were very helpful. To start creating a gem with bundler you need to type bundler gem [name of gem], this creates a:
- gemspec file — info about your gem and dependencies
- spec folder — your tests — no different to a normal ruby project
- lib folder — your code — no different to a normal ruby project
- bin folder — make it executable (optional) — my core logic gem didn’t have an executable file, but my command line game and my web game both did. This just sets up the ability to run the app by typing a string into the command line
- various other files like a rakefile, license, readme, code of conduct (optional), etc.
Publishing to RubyGems.org is quite straightforward, it involves creating a RubyGems.org account, pushing your repository to your own Github account, and running rake release. It does everything else for you.
Now I have the following gems published for anyone to use:
