Welcome
Hello, dear friends, Ruby enthusiasts, and coders. I am glad you found time to spend your precious time to learn a bit more about precious Ruby and its precious “Gems”. So, let’s get to it.
What are Ruby gems?
A Ruby gem is a package that can be downloaded and installed as a part of your application you’re developing. They actually contain a packaged Ruby application or library. When you require an installed gem you’re adding extra functionality to your Ruby program.
Gems can be used to extend or modify the functionality of your Ruby applications. What is great about them is that they can be distributed and shared with other Ruby coders so that they can use them in their own applications.
Examples of gems
Gems make our life much easier, to the extent that we can save not hours, but weeks or months of coding time. By just adding the required gem to our application, we can quickly and easily integrate the feature it provides to our fresh baked application.
Here are some of the extra functionalities, ruby gems can provide for our applications:
- Authentication (Devise, ruby-jwt, omniauth, etc)
- Authorization (cancancan, pundit)
- Testing (Rspec, shoulda-matchers, capybara, faker, etc.)
- Search (elasticsearch)
- Admin panels (activeadmin, administrate)
- and hundreds and thousands of more and more…
Where can I find them?
You surely know about npm — node package manager and that it is a central point where you can search for and learn about javascript redistributable packages.
For Ruby gems, you can go to the RubyGems website. It is the Ruby community’s gem hosting service, where you can find, learn about, install them and publish your own gems.
There is yet another service where you can search for gems by category. It is “The ruby toolbox”, and it is a catalog of gems that keeps track of popularity and health metrics to help you choose a reliable library.
Managing Ruby Gems
Of course, to manage the gems you use in your application you need a package manager. Something should be keeping track of the gems you installed and their versions. That’s where the Bundler comes in. Well, Bundler is a tool for dependency management. It provides a consistent environment for your projects by tracking and installing the exact gems and versions that are needed.
Usually, you list the gems you use in a file called “Gemfile”. That’s the starting point where the Bundler analyzes and installs all the required gems to be used in the application.
Here are some of the commands you would use to manage your gems in a project:
- gem list: Lists installed gems. Accepts an argument for filtering gems by name (example: gem list active)
- gem install -v : Allows you to install a specific gem version (example: gem install sinatra -v 2.0.0)
- bundle install: Installs all the gems listed in the Gemfile
- bundle update: Installs all the gems, and if required, updates specific gems to the latest version available.
Conclusion
Before using a specific gem, please read its docs and analyze it thoroughly. The thing you should be considering is whether it exactly meets your requirements and whether it does not break your functionality when you decide to extend some of the features in your application.
I mean it must not be a burden when you decide to extend some functionality or add a new feature to your application. If you are suspicious that it can, then search for another one that fits best for it. For that, you have to carefully read the docs it provides on its website or its GitHub Readme file. It is better if you try using it in a small sample application, before integrating it into your desired application.
Here we come to the end of our article about Ruby gems, how to cook and how to eat them. I hope so much, it can be of use for anyone interested in Ruby, or maybe especially Ruby on Rails.
Top comments (1)
Thanks for the article. May I suggest to add an explanation for
Gemfile.lock
? It's basically the same as whatpackage-lock.json
is forpackage.json
and you should definitely check it in (in git).I guess the next step is a tutorial about developing your own gem. Here and here is some inspiration.