Gemfile basics and useful gems

Mateo mojica
5 min readJan 13, 2023

--

Photo by Dan Farrell on Unsplash

When starting with a new framework there are a lot of concepts and nuances that you have to learn and it can be very overwhelming. In this article, I’m going to explain some concepts when starting with ruby on rails so you can tick that box in your checklist and don’t have to worry about learning these concepts again.

Let’s start with gems. A gem is a package containing ruby code that you can include in your application and use. Ruby uses a package manager called RubyGems and its command is gem to access it. Depending on the language and framework there are different package managers, some of them are npm for node, pip for python, and NuGet for .NET.

Now that we know what is a gem let’s see some tips to choose them. There are many gems to select from, which makes choosing one very complicated. To select a good gem you have to check several things: First, you need to understand the functionality that you want to incorporate into your application so you can start searching for gems that do that, so for example if you need to create pdfs in your application that is what you should look for in a gem. Second, you need to check if the gem that you have your eye on is compatible with the version of ruby and other frameworks that you have in your application, sometimes some gems only work with a specific version of ruby or rails. The third aspect of selecting a gem is that it is supported, you want to check when was the last time the gem was updated and how many downloads it has, because if the last time the gem was updated was 5 years ago, if you run into a problem with it chances are that you are not going to get any official support and the topics on that gem are going to be very limited. From time to time that last recommendation has to be dismissed because there is only one gem that has the functionality that you need, so be aware of the risks you are taking by selecting an outdated gem.

Photo by Mildlee on Unsplash

Now that we know the basics for gems, now let’s talk about where they live. The gem file is a file in your application that has in it all the gems that you want to include in your project, you can have different sections for different environments (development, test, production) with gems that are going to be available just for that environment or you can have general gems that are going to be in your application regardless of the environment. When you find a gem in rubygems.org it will have the command to install it directly in your machine or the line to include it in your gemfile. It is not required but highly recommended to include a version of your gem when you are including it in your gem file so that way you have control over the code that you created using the gem, so in a far distant future when you have to reinstall the gems for the project you don’t encounter any issues with your code due to an updated version of one gem.

Photo by iMattSmart on Unsplash

There is another file that is generated when you install all the gems in your gemfile called gemfile.lock. This file specifies all the gems that are needed to run the application, so it will have all the gems from your gemfile but will also have all the gems that those gems are dependant on that were also installed with their versions, so if you don’t know which version of the gem you should put in your gemfile this is a good source to get it. Important side note, this file should never be modified manually, it should only be modified by changing the gem file and then running the bundle install command.

And finally, let’s see some helpful gems that I use on a regular basis for developing applications or features:

  • Rack-cors: This gem will save you a lot of headaches when creating APIs using rails because it configures the CORS (Cross Origin Resource Sharing) for your API so the front end connecting to it can clear the information you send.
  • Faker: This gem will fake data from different industries and applications (for example medical or financial) so you can have your mock data easily filled in. This is very useful for tests.
  • Fabrication: Very useful gem for writing tests, it creates data for models based on initializers that you create for each model.
  • Timecop: This gem will help you time travel or freeze time so you can test several time-related features, like token expirations. Very useful for testing.
  • Mini magick: Manipulates properties of images, it has a very extensive list of properties that it can change on an image that will help you standardize all the images in your application. Very useful for uploaders and user-uploaded images.
  • Pry bye bug: This is a great gem to debug your rails code. What it does is set a breakpoint anywhere you put a binding.pry statement and then it gives you a console to see and control the flow of execution of the code.
  • Dotenv rails: Auto loads your .env file into your rails application to keep your secrets in a different file.
  • Rubocop: Code checking and formatting. Very useful for enforcing standards.
  • Pluck to hash: Useful to pick columns from ActiveRecord results and put them into hashes or arrays depending on what you need.
  • Jwt: Gem to create JWT tokens and to decode and validate them as well.
  • Active interaction: This gem allows you to easily create service objects to separate your business logic with a lot of options and filters if you need them.

Thank you for reading all the way through this article, I hope it was helpful and clarified some concepts for you. If you liked it give it a clap and check out my other articles.

References

--

--

Mateo mojica
Mateo mojica

Written by Mateo mojica

Electronic engineer and software developer

No responses yet