Work with Gems - how to choose

Ruby provides a variety of Gems for every requirement and new developers get confused when choosing the between Gems.

So I’ll share my personal guidelines for choosing a Gem:

1. Check if the Gem is maintained:

You can verify if the author mentions in the documentation that this gem is not actively maintained for example Test Benchmark Gem, so in this case, it’s better to look for another alternative.

Another option is to check the interaction of maintainers in issues section, don’t be surprised if you find a popular Gem with +100 open issues.

2. how much functionality of the gem will you use?:

This is a very important question, if I’ll use more than 60% of available functionalities then I install the Gem. Otherwise, I’ll implement the functionality.

That’s why I prefer minimalist Gems over big ones, it’s easier to integrate and reuse it in different use cases.

3. Ask a senior Developer:

That’s right, don’t spend more than 10 minutes to choose a Gem, if you get confused ask your senior colleague or reach out to the community online. Ruby has a friendly online community and people are willing not to only help you choose, but they will provide explanations 🙂

4. That’s all?:

There are other factors for choosing a Gem, like the provided DSL or the integration pattern in an application but I believe that’s a personal preference. And after working on different projects you will start developing a list of favorite Gems.

Now after choosing a Gem,  it’s time to add it to your project, so the most important note is:

Note:Do not call a Gem directly from your project code

Instead of calling your new Gem directly think about adding a wrapper class around the Gem API. This approach is called also the Adapter pattern. Let’s use Twilio Gem as an example:

#app/adapters/twilio_adapter.rb

class SmsAdapter

  attr_reader :client
  def initialize
    @client = Twilio::REST::Client.new account_sid, auth_token
  end

  def send_sms
    client.messages.create(
      body: "Hello from Ruby",
      to: "+12345678901",
      from: "+12345678901")
  end

end

The benefits from the above example are:

  • We can easily change Twilio Gem based on requirement
  • The adapter provides an API that fits the application domain
  • Have tests that aren’t dependent on the Gem

That’s it, happy coding 🙂