Make gem

Engineering a Ruby gem for beginners. 


Writing code is hard. You have to solve a problem while writing efficient and understandable code — plus the hard part: comments. You might know the saying:

Write code like your future self it’s behind you holding a knife.

imagine that you have a few more people behind you, things get rough.

Writing Foregit with the community in mind proved to be more difficult than I thought. I browsed through a few gems and with the help of my mentor, I started building a gem.

Advice: Don’t try to get it right from the first try — this goes beyond engineering. Even if libraries usually have a pattern, things are different and you shouldn’t exchange value for practice.


How to write a Ruby gem?

Step one: Pick a good name for your gem. Don’t judge a book by its cover, but choose a gem by its name. ☺

Step two: Think of a license and add an introductory “Readme.md”, even if the project is empty, talk with your audience.

Step three: Add a “Gemfile” to your project, it’s easier for development and keeping track of dependencies.

Step four: Structure your gem by adding:

  • a file called “<gem_name>.gemspec” which you will use to build, install and publish your gem.
  • a “lib” directory under which you will write the actual gem. And a “lib/<gem_name>.rb” file where you should import all of your library files (at least).
  • a “test” or “spec” directory, yeah tested code ☺.
  • a “doc” directory for all the documents you want to attach as part of the project documentation. You can also generate live documentation from your code base, that’s even better.
  • a “Rakefile” — at least define a task to run your tests.

Step five: Integrate with Travis CI. You wrote tests, show all that your project is stable. A nice touch is to add the project icon to the main “README.md”.

Step six: Add additional directories, like “config” or “bin”:

  • “config” is used to store configuration files, but don’t make your settings public, write an example file.
  • under “bin” store executable files.

Tip: Add all of this directories and dependencies in “<gem_name>.gemspec”.

Reference: The best article I found about this subject is http://guides.rubygems.org/make-your-own-gem, followed by http://guides.rubygems.org/patterns. All gems are found here http://rubygems.org.

Tip: Reuse as much as possible, don’t reinvent the wheel. Ask for constant feedback, if you don’t have a collaborator find one.


At the end of the day it’s important to have an easy to use gem.

If you want to be a superstar and if you use GitHub, create a GitHub page for your gem, it’s easier to communicate with the users. Remember: more users, more contributors.

Oh, yes — publish your project on a website like GitHub. But you already knew that. ☺

Spor!