What’s a gem anyway?
What is a gem? According to RubyGems.org, “Each gem has a name, version, and platform.” That’s literally the first sentence of its answer to that question. Clear as mud.
Simply put, a gem is a packaged Ruby application or library. That’s all. It’s just Ruby code, arranged in a format that’s easy to share.
Gems actually exist apart from RubyGems (i.e., you can have your own internal gems), but RubyGems is the package that became part Ruby’s standard library (in version 1.9 of Ruby). With RubyGems, developers can search, install, build, and publicly share gems straight from the command line. When you type
gem list (or any other
gem command into the command line, you’re depending on RubyGems. Typing
gem -h, you’ll see that “RubyGems is a sophisticated package manager for Ruby,” so I suppose we can add pretentious to the list of RubyGems’ attributes.
My own exploration
In the interest of learning by doing, I made a gem. The process is straightforward; I followed the RubyGems guide, which lays out each step quite clearly. It comes down to the following:
- Name your gem name, write your code, and put it in a ‘lib’ directory.
- Create the gemspec (named [gem name].gemspec). The content follows a clear template specifying the gem’s name, version, date, etc.
- From the command line, run
gem build [gem name].gemspec
- Once built, run
gem install ./[gemfile name](the gemfile will end with the “.gem” extension and will be included in the return value of the build command.
curl -u [RubyGems username] https://rubygems.org/api/v1/api_key.yamlto connect to RubyGems.org (it’s very easy to create a RubyGems account).
gem push [gemfile name]
I could’ve done a simple “Hello World” example like the one in the guide, but I wanted to make something more “useful.” I could describe it here, but you should just try it out isnstead:
gem install ny_forecast.
I now have an extra layer of illusive legitimacy, with my NY Forecast gem on RubyGems.org. It means there’s not so much separating me from a programmer like DHH! Except maybe 23 extra gems and 250 more million downloads…