Building a Ruby CLI Gem From Scratch

Let me start by saying that one thing I understand now is, as programmers, we don’t need to recreate the wheel, there are a lot of tools we can use to make our life easier in terms of building our projects. For this particular project Bundler will create our gemspec file by simply typing:

gem bundler <gem name>

in the command line, basically stubbing out all the structure in advance automatically (which can be very tedious if done manually). Once we have our gem file in place, let the coding begin…


First I created the executable file that ultimately the user is going to interact with, and then I started programming all the objects and methods needed for my CLI to work. (Don’t forget to give executable permission to your bin file).

#!/usr/bin/env ruby

I try to solve one problem at the time and write code little by little, I’ve learned that trying to make everything work all at once is simply impossible. From here on I started applying something I learned from Avi Flombaum:

Write the code you wish you had

The Challenges

Patterns For Requiring Libraries

One of the first challenges I encountered was the different patterns for requiring libraries, and after trying out several ones I came down with the following:

For my bin file:

require './lib/cards'

For my environment file:

require 'open-uri'
require 'nokogiri'
require 'pry'
require 'colorize'
require 'table_print'
require_relative "./cards/version"
require_relative "./cards/credit_card"
require_relative "./cards/scraper"
require_relative "./cards/cli"

keep in mind the importance of the order of execution, if not done properly, your gem will break.

For my gemspec and dependencies:

spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_dependency "nokogiri"
spec.add_dependency "colorize"
spec.add_dependency "table_print"

Once I managed to have all my dependencies working, I went onto my next task.


Scraping can be overwhelming at first, but after you do it for a while, it becomes second nature and even fun, if you have a firm grasp of CSS and HTML it won’t be much of a task, Ruby gsub and strip methods can be your best friends while scraping.

The Takeaway

Understanding object collaboration is game changing, once you realize that each object should have its own methods and it should do a specific task, programming becomes easier, I won’t lie I had to re structure my gem several times, erase methods and create new ones in the right place, but It’s all part of a learning process, the more mistakes you make the more you learn, so be curious, explore and remember that any question you have, someone probably answered it already, google everything, challenge your self, at one point we all get frustrated, but there is no better feeling than seeing your work finished.

giphy (3)

Here a full video of how I built it.


Adrian Prieto

“If you are not willing to learn, no one can help you. If you are determined to learn, no one can stop you.” ― Zig Ziglar

Latest posts by Adrian Prieto (see all)

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *