Gems and Gemfiles

James Berry
3 min readAug 4, 2021

--

Bundle install. Enter.

Your terminal is populated quickly with a cascade of file names and numbers, ending with that sweet sugar coated green text that we all know, love, and now certainly crave. Your bundle has been completed and you now how some number of gemfile dependencies as well as many more gems now installed, but what exactly are those gems and gemfiles?

To start with the smaller item, the gem is a collection of Ruby code, essentially a packaged together ‘bundle’ of code containing either a single application or an entire library. Since redundancy is at the heart of both the internet and of coding, having these bundles becomes incredibly time saving and useful as it allows programmers to utilize already made blocks of Ruby code.

A Gemfile is a file we, the creators of the gem, create and which is used for describing gem dependencies. All gems require certain systems to be able to run. The gemfile is going to ensure that when you install a gem that your system understands exactly what is needed for your gems to be able to run inside of your program.

Now that we have a very cursory understanding of gems and gemfiles, let’s talk about exactly how a gem is made. The most basic type of gem is going to have at least two parts to it, your gemspec and your .rb (Ruby) file. Your gemspec is going to contain all of the information about the gem. This includes its name, author, what version it is running on (gems are frequently updated and modified), and importantly a link to the homepage where the gem is actually stored.

Now let’s look at exactly what is going to go inside our .rb file, in other words the gem itself. For the most part, this is going to look like any other Ruby method that you have created.

You will have
% somewhere lib/(file_name).rb (which directory we are in)

Class: Class_Name (your class name)
def self.name (your method name)
puts “James!” (your code)
end
end

Now that you have your gem code, you’ll need to create your gemspec, which is going to contain the specifications about your gem. The gemspec can contain a pretty wide array of tags apart from the few I mentioned above, for a complete list follow this link: https://guides.rubygems.org/specification-reference/. Roughly speaking, your gemspec is going to follow a pattern that is actually fairly similar to table-migrations using active record.

%somewhere name.gemspec (which directory we are in)
Gem::Specification.new do |s|
s.name = ‘name’ (your gem name)
s.version = ‘1.0.1’ (which gem version you are running)
s.description = ‘puts ‘James’ into the terminal’
s.author = [“James Berry”] (notice the array braces)
s.email = ‘james@berry.fbi’
s.files = [ “lib/name.rb”] (notice again the array braces, you can and usually do have more than one file in this gemspec)
s.homepage = ‘https://rubygems.org/gems/name’ (this is the link to where your gemfile is actually stored)
s.license = ‘FLATIRON’
end

Once this is done, you can build your gem by typing: %build name.gemspec
which will build the specs of your gem. Afterwards you can run:
% gem install ./name-1.0.1.gem and that will install your gem.

Congratulations, we’ve built a gem!

From here there are two more, fairly small steps to invoking your gem and actually using it. The first step is to actually have a program ‘require’ your gem.
% irb (interactive ruby shell)
>> require ‘name’
=> true
>> Name.name
“James!”

After you’re satisfied that your gem can actually running when it is required. You can upload your gem onto the rubygems database* as long as you have an account on the rubygems database. All you need to do us run:
curl -u username https://rubygems.org/api/v1/api_key.(insert_key_here) > which will prompt you to log in with your username and password.

* https://rubygems.org is a massive database of knowledge and gem where you can upload your own created gems.

If you’ve done all that correctly, now you can share your gem with the Ruby community!

--

--