Ruby Gems : a programmers love story

Gracie McGuire
6 min readDec 14, 2018

Over my extremely short tenure as a novice software engineer, I as, most students learning Ruby have installed a plethora of the elusive ‘gems.’ All I knew was gems were some code, written by another developer, that you can simply install to save time. Pretty much, the idea is don’t reinvent the wheel if you don’t have to. In the first few months I was learning to develop, I really leaned into some of the more magical aspects of software engineering; type this one thing and a million things happen? Rad. Mostly, I found myself so insanely overwhelmed by the firehose of information pointed at my face that when something just ‘worked’ I didn’t dare question. The more I learn though, I find myself questioning more and more of these ‘magical’ aspects of programming. Nothing really comes out of thin air, right? I knew gems were built by some program wizard in a far off land, but only recently started to wonder how this process actually worked.

Enter: Mod 2 at Flatiron School of Programming & Wizardry. My intention for this second module is to never chalk anything up to just working… If something is executed in my program, I at least want to understand the basic overview of how it works. As I began researching the basics of ruby gems, I realized my kinesthetic tendencies weren’t going to comprehend much of anything unless I actually created my own. Through a mix of several different how-to’s and blog’s I made my own gem that is available for download at https://rubygems.org/gems/flatiron_best.

If you have ever worked in Ruby you have undoubtedly runbundle installin your terminal, and seen a ton of things pop up in green, giving you an odd feeling of undeserved accomplishment… but what does bundle install actually mean? If you check out your Gemfile there’s typically a ton of gems already in there, as well as the term require, which you will most likely see in multiple directories in your program. Require was initially built in the late 90’s as a method that would be used across a program in multiple places, however writing a basic require method wasn’t practical because the method would overwrite itself over and over again, so there had to be a place to store the information already required.

$not_gem_file_yet = []

def require(not_quite_a_gem)
return true if $not_gem_file_yet.include?(not_quite_a_gem)
eval File.read(not_quite_a_gem)
$not_gem_file_yet << not_quite_a_gem
end

The problem above is that the not_quite_a_gem file would be super difficult to share with other developers, or even other programs for a single developer. Developers created multiple programs over the next few years that don’t exist anymore because in 2004, RubyGems was created! Pour some out for RAA and setup.rb real quick though for laying the groundwork before their sub-sequential falls. As RubyGems came to save the day with one single command gem install, they brought with them a ton of other features Ruby programmers use every day, including fixing the require method andrack.rb.

It wasn’t all easy though, as more and more developers began using gems, it became apparent that the RubyGems feature that used the most recent version of each gem upon install was causing a plethora of problems as code was written under the pretense of multiple versions of gems, across different programs, and under different developers. This led to developers spending less of their time building and creating, and more time prying into why some programs would work on one computer and not another, or how when a gem was installed in a new program, how all the other programs on that computer would suddenly break.

This tedious and annoying process led to the creation of Bundler. What happens when bundle install is run is pretty cool. Bundler goes through and checks every version of the gem that work with each file on the GemFile site, and writes them into the Gemfile.lock for future installs. Then, when you run bundle install from another computer, it installs every gem and version stored in the Gemfile.lock.

A ruby gem really is just bits of code that do things, no matter how big or small, if you add a couple of extra ‘require’ lines and spec files, then push to rubygems.org, anything can be a gem. My gem “flatiron_best v 1.0.1” was the 28,930,327,759th ruby gem to be uploaded… groundbreaking.

Version numbers in gems follow the convention of major.minor.patch. Major represents major changes made to the gem, minor represents smaller changes like functionality, and patch represents fixing errors and bugs.

The code for flatiron_best v 1.0.1 simply informs the user that “Flatiron is the best!!!” if in your Ruby file you call “Flatiron” or “flatiron” as the parameter to your School Class variable, if you pass it anything else it will inform you that anything else is simply “Sub par…”.

Step 1. Make sure you have bundler! In your terminal, run gem install bundler.

Step 2. Name your gem! Apparently some people struggle with this as there was ample documentation on how to properly name your gem… But since my gem has no purpose, I skipped this step and just ran bundle gem flatiron_best.

Step 3. Check to make sure everything is there — your gem needs a Gemfile containing a gemspec, Rakefile for easy build, install, release tasks. Code of Conduct, License, and Lib which will contain your gem’s module, classes, environment, and version constant.

Step 4. Create a new directory for your specs, so we can test the gem as we go. In here build out the RSpec test cases, then require this in your lib folder.

Step 5. Now that you’re all set up, open up that lib file, this is where all of the code goes, the actual root of the gem!

Step 6. Once you have your lib and directory’s set up its time to test!

Step 7. Setup your Rspec tests and test away… once these are good to go you are all ready to upload.

Step 8. If you already have a RubyGems account, run curl -u and sign in. This will prompt you to the next steps.

To answer my original question, what the heck really is a Ruby gem? Gems are shared bits of code made to help execute any program quickly, cleanly and efficiently. Nothing magic about them, except the power of sharing and community. Cute.

Check out these sites for more details on RubyGems!

--

--

Gracie McGuire

NYC based software engineer. she/they </racism> </patriarchy> graciemcguire.com