[TIL] Debugging external gem in Ruby

yowchun93

YC Yap

Posted on February 4, 2020

[TIL]  Debugging external gem in Ruby

Sometimes there comes a time where an external gem that you installed is not behaving as expected. It was not until a week ago that i learned from my colleague that you can actually open the gems to inspect the code inside the gem and debugging it using a debugger.

Firstly, you can open the gem by running.

bundle open countries

Identify the part of the code which you wish to debug. For example i would like to understand why a method in the gem 'countries' is not returning values as expected.

Start inserting calls to debugger in places you expect it to call.

require 'byebug'

module ISO3166
  class Country
    def initialize(country_data)
      byebug
      @country_data_or_code = country_data
      reload
    end
  end
end

Using debugger allows you to better understand the code execution, so that variables which are causing the unexpected behaviours during the execution call be be inspected.

💖 💪 🙅 🚩
yowchun93
YC Yap

Posted on February 4, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related