Ruby gems are bits of code that are packaged up to add features to your application, or to be used as standalone programs. There are popular gems that let you interact with Etsy or Twitter, build a web app or to run background jobs. These can all be found on Rubygems.org, which is the most popular and widely accepted source of Ruby gems on the web. Alternatively you can set up a private gem server to host private gems for a company or organization.
The purpose of gems is to allow you to write code once and use it over again in many places with minimal hassle. For instance if you wrote a bit of code that let you integrate with Etsy’s API for one app you are making you won’t want to have to rewrite it again for another app you are making. Because of this many gems are created by extracting code from a current project when it becomes apparent that the code would be useful in another project.
This method of reusing code is much better than copying and pasting code form app to app. It allows us to find bugs in the code once and fix them once instead of many times over many different apps. It also lets you open source a small portion of your app without giving away all of the secrets in your code. Also it means that you don’t have to write an integration with Etsy since Katrina Owen already wrote one for you.
Bundler is a very common way to manage your gems in an application. In Rails we use it to “bundle install” all of the gems listed in our Gemfile. We have a very large open-source ecosystem of gem available for anyone to use freely in Ruby, but there is always room for more gems and improvement of the existing gems.
You can also use gems to do interesting things like turning a whole Rails app into a gem. Gems and the open source gem ecosystem lets you write app much faster than you might have been able to normally since many parts of the application that you might have had to build out yourself have instead been built out and tested by others allowing you to simply add them to your system.
The Purpose of RubyGems.org
The biggest (and defacto) repository of all of these gems found online is a Rubygems.org. As of this writing there are 77,000 gems that have been ‘cut’ and over 3 billions downloads from their service. When you build a gem you should be sure to add it to Rubygems.org. Of course sometimes you don’t have that option. Maybe you are building your gem as part of simplifying a private corporate app that you are working on. There are many good and valid reasons for keeping code private or not sharing your code outside your organization. In this case you can still pull your code out into gems and deploy them to a private server or private repos on Github which allows you to keep the code secret. You still have the advantage of DRYing up your code as well as being able to use the same code in multiple apps. You can use this to create a service oriented architecture for your apps and reduce build cost across many of them.
Learn more about Ruby Gems: Practical Ruby Gems