DEV Community

Cover image for Intro to Ruby Gems
Brandon Brown
Brandon Brown

Posted on

Intro to Ruby Gems

Coming into the Ruby software development world can be daunting. At least this is how I felt when I first started my Ruby coding adventure. As a beginner to this wonderful language, there seemed to be endless topics to learn, countless questions to ask, and thousands of concepts to master. It was around here where I sat back and wondered how anyone can master everything with the Ruby language. As my education and exploration continued, I ran across the concept of RubyGems and realized, “Huh, maybe I don’t have to master absolutely everything.”

RubyGems, huh? Sounds cute or maybe a little gimmicky. Despite this initial feeling, diving deeper into the topic opened my eyes to a whole new world. Exploring the internet for these little guys, it appeared that I could solve a whole host of problems I knew nothing about hours ago. From creating your own web applications to just printing things prettier on the console window, it looked liked these guys could do it all.

Hopefully I have captured at least a small bit of your attention. So what are these magical things anyway? When do I use them? Where do I get them? How do I get them? How do I use them? These are the same questions I had initially, and I will try my best to answer all of these.

What is a Ruby Gem?

In short, a RubyGem is a library of code containing classes, modules, methods, and possibly more. Once created, they are packaged up by their creators for easy distribution to fellow Ruby developers. This sounds extremely generic, however, RubyGems are not bound by one functionality or one goal. They are simply code created to accomplish a task.

When do I use them?

RubyGems are widely applicable to most everything you would like to accomplish in Ruby. Most of the time, the short answer for using a Gem is to Google the issue you’re having, and search around forums and websites for interesting articles. For instance, you may want to find some way to effectively and quickly write code tests and stumble across a wonderful Gem called Rspec. Or you may come across ActiveRecord by trying to find an easy way to read and write data to databases without using those nasty SQL queries. In short, if you have a problem you’re looking to solve, Gems could be the answer.

However, Gems should not be the answer 100% of the time. Below are a couple of example of when to not use a RubyGem.

  • For accomplishing relatively simple tasks, most of the time, Gems should not be used. Be wary of spending hours looking for Gems when spending 30 minutes or so critically thinking could also solve the problem.

  • RubyGems are loaded into memory upon execution and could slow down the development and testing process with longer loading times. This is especially problematic with very large or poorly optimized Gems.

  • RubyGems are maintained externally and can change over time. This leaves your project as the mercy of the Gem developer(s).

Gems are still wonderful and powerful tools with a great community behind many of them. Next we will explore where to find them.

Where do I get them?

Although it is possible to get RubyGems elsewhere, the most popular place to get them is rubygems.org. Once there, just type in the Gem you are interesting in using and press search. In this example, we are going to be looking for the awesome_print Gem which prints data to the terminal in a more readable fashion.

Ruby Gems Homepage

After searching for a Gem, a list will pop up showing the Gems that apply to your search.

Ruby Gems Search

From the search results, select the one you would like to download. In our case, we would pick the first one: awesome_print. Click the Gem link, and you will be navigated to a page similar to the one below.

Gem Page

Keep an eye on the INSTALL: section. We will be using this to install our Gem.

Installing Ruby Gems

The following install instructions are for Mac OSX users with Ruby 2.0 or later. To install a RubyGem, simply open the terminal application and copy and paste the install command mentioned earlier.

Installing Gem

If completed successfully, you should see a message saying “1 gem installed.” We can verify that this Gem installed by checking our installed Gems by running “gem list” in the terminal.

Installing Gem 2

Hooray! We have successfully installed awesome_print! Now we just need to know how to use this little guy.

Requiring and Using the RubyGem

Now that we’ve got our super awesome Gem installed, using it is just as easy. To start using this Gem, first we need to require it. Since the Gem is installed on our machine now, we just need to type “require ‘awesome_print’” inside of our Ruby file. This will tell our computer to load the awesome_print Gem during the execution of our file. Make sure to require the Gem before you try to use that Gem. Not doing so will result in an error.

require 'awesome_print'

Now let’s make some dummy data that we can print out.

require 'awesome_print'

employees = [{ name: "Jimmy", position: "Software Engineer 1" }, { name: "Bob", position: "Software Engineer 1" }, { name: "Mark", position: "Software Engineer 1" }, { name: "Beth", position: "Software Engineer 4" }, { name: "Nancy", position: "Software Manager" }, { name: "Stacy", position: "Software Engineer 1" }, { name: "Earl", position: "Maintenance 1" }, { name: "Alfred", position: "Security Officer" }, { name: "Bruce", position: "Senior Accountant" }, { name: "Robin", position: "Assistant to the Accountant" }, { name: "Cory", position: "Software Engineer 2" }, { name: "Grant", position: "CEO" }, { name: "Geoffrey", position: "CTO" }, { name: "Wendy", position: "CFO" }, { name: "Moxxie", position: "Quality Assurance Manager" }, { name: "Gustave", position: "Foreign Relations Advisor" }, { name: "Robert", position: "Engineering Manager" }, { name: "Brittany", position: "HR Manager" }, { name: "Hercules", position: "Senior Security Guard" }]

Oh man, that looks really messy. Hopefully awesome_print makes it a little easier to read for us. Let’s employ it by using the “ap” method defined in the awesome_print Gem.

ap employees

Finally, let’s run this code to see what we get in our terminal.

[
    [ 0] {
            :name => "Jimmy",
        :position => "Software Engineer 1"
    },
    [ 1] {
            :name => "Bob",
        :position => "Software Engineer 1"
    },
    [ 2] {
            :name => "Mark",
        :position => "Software Engineer 1"
    },
    [ 3] {
            :name => "Beth",
        :position => "Software Engineer 4"
    },
    [ 4] {
            :name => "Nancy",
        :position => "Software Manager"
    },
    ...
]

Lo and behold! The power of awesome_print! I will mention that for the sake of this blog, I cut out a majority of the information that awesome_print supplied. Awesome_print does in fact display all of the employees, however, I figured listing 5 employees was enough to get the point across.

Conclusion

Hopefully this has been a good introduction to using RubyGems. We have explored what a RubyGem is, when to use one, where to find them, and how to use them. From now on, I hope you feel confident in your ability to find and and install these wonderful tools. Who knows? Maybe one day you’ll be making your own Gems and sharing all of your wonderful work with others! Thank you for reading my blog and happy Gemming!

Top comments (0)