Finding source of possible Ruby monkey patch causing troubles?

181 Views Asked by At

I’m dealing with an error that I have been yet unable to debug. I had an application with 100% green tests in Rails 6.0. I tried updating the app to Rails 6.1 and now I’m seeing the following behaviour:

include ActionView::Helpers::NumberHelper
number_to_currency 7
TypeError (no implicit conversion of String into Integer)

Ruby 2.7.3 Rails 6.1

There are a lot of gems and I’m wondering if this is related to somebody monkey-patching something but I don’t know if there’s a way to trace a source of the money patching? Or if you have any ideas I would appreciate any advice.

Here’s the backtrace:

number_to_currency 7
NoMethodError: undefined method `nan?' for nil:NilClass

   5  module ActiveSupport
   6    module NumberHelper
   7      class NumberToRoundedConverter < NumberConverter # :nodoc:
  11        def convert
  24          if precision = options[:precision]
  31            formatted_string =
❯ 32              if rounded_number.nan? || rounded_number.infinite? || rounded_number == rounded_number.to_i
  34              else
  40              end
  41          else
  47        end
  62      end
  63    end
  64  end
1

There are 1 best solutions below

0
brainbag On

By using the pry-rails gem, you can most likely find the source by putting in binding.pry just above where the error occurs, and then re-running the code. When the Pry REPL comes up, you can then run the command show-source number_to_currency. This will show you the file location, for example:

[2] pry(main)> show-source number_to_currency

From: /Users/.../actionview-6.0.2.2/lib/action_view/helpers/number_helper.rb:70:
Owner: ActionView::Helpers::NumberHelper
Visibility: public
Signature: number_to_currency(number, options=?)
Number of lines: 3

def number_to_currency(number, options = {})
  delegate_number_helper_method(:number_to_currency, number, options)
end

If you then need to go up the definition chain, you can also use the --super flag multiple times to go up until you find the issue. For example, to go up 2 supers, you would do show-source --super --super number_to_currency

You can see more options with show-source --help