Skip to content

Commit

Permalink
Let Correctable#original_message skip prepended method definitions
Browse files Browse the repository at this point in the history
Previously, DidYouMean::Correctable#original_message did
`method(:to_s).super_method.call` to call the original to_s method by
skipping Correctable#to_s.

I'm now creating a gem that prepends another to_s method to NameError,
which confuses the hack. An immediate solution is to replace it with
`method(:to_s).super_method.super_method.call` to skip the two methods.
But it is too ad-hoc.

This changeset uses more extensible approach and allow a prepended
module to declare that they should be skipped by defining a constant
named `SKIP_TO_S_FOR_SUPER_LOOKUP`.
  • Loading branch information
mame committed Jun 18, 2021
1 parent fbe5aaa commit 8352c15
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/did_you_mean/core_ext/name_error.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
module DidYouMean
module Correctable
SKIP_TO_S_FOR_SUPER_LOOKUP = true
private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP

def original_message
method(:to_s).super_method.call
meth = method(:to_s)
while meth.owner.const_defined?(:SKIP_TO_S_FOR_SUPER_LOOKUP)
meth = meth.super_method
end
meth.call
end

def to_s
Expand Down

0 comments on commit 8352c15

Please sign in to comment.