Building Ruby Gems

Dan Pelensky
Jul 10, 2017 · 2 min read

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:

  • ttt_core — a gem that has the core logic of a tic tac toe game, can be used with a custom front end
  • ttt_cli — a command line app for playing tic tac toe. Uses the ttt_core gem for the logic
  • ttt_web — a web app for playing tic tac toe. Uses the ttt_core gem for the logic

Written by

Software Crafter. He/Him. Views are my own

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade