[前][次][番号順一覧][スレッド一覧]

ruby-changes:66623

From: Yusuke <ko1@a...>
Date: Tue, 29 Jun 2021 17:28:07 +0900 (JST)
Subject: [ruby-changes:66623] 612b6fcd37 (master): Let Correctable#original_message skip prepended method definitions

https://git.ruby-lang.org/ruby.git/commit/?id=612b6fcd37

From 612b6fcd371adc199fb4503941edfdbbac704ef4 Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Fri, 18 Jun 2021 17:32:47 +0900
Subject: Let Correctable#original_message skip prepended method definitions

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`.

https://github.com/ruby/did_you_mean/commit/8352c154e3
---
 lib/did_you_mean/core_ext/name_error.rb | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lib/did_you_mean/core_ext/name_error.rb b/lib/did_you_mean/core_ext/name_error.rb
index 77dcd52..74febcc 100644
--- a/lib/did_you_mean/core_ext/name_error.rb
+++ b/lib/did_you_mean/core_ext/name_error.rb
@@ -1,7 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/lib/did_you_mean/core_ext/name_error.rb#L1
 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
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]