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

ruby-changes:53704

From: nobu <ko1@a...>
Date: Thu, 22 Nov 2018 17:29:09 +0900 (JST)
Subject: [ruby-changes:53704] nobu:r65920 (trunk): Enable refinements at Object#respond_to?

nobu	2018-11-22 17:29:02 +0900 (Thu, 22 Nov 2018)

  New Revision: 65920

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65920

  Log:
    Enable refinements at Object#respond_to?
    
    [Feature #15327] [Fix GH-2020]
    
    From: osyo-manga <manga.osyo@g...>

  Modified files:
    trunk/NEWS
    trunk/spec/ruby/core/module/refine_spec.rb
    trunk/test/ruby/test_refinement.rb
    trunk/vm_method.c
Index: test/ruby/test_refinement.rb
===================================================================
--- test/ruby/test_refinement.rb	(revision 65919)
+++ test/ruby/test_refinement.rb	(revision 65920)
@@ -326,9 +326,9 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L326
     end
   end
 
-  def test_respond_to_should_not_use_refinements
+  def test_respond_to_should_use_refinements
     assert_equal(false, 1.respond_to?(:foo))
-    assert_equal(false, eval_using(IntegerFooExt, "1.respond_to?(:foo)"))
+    assert_equal(true, eval_using(IntegerFooExt, "1.respond_to?(:foo)"))
   end
 
   module StringCmpExt
Index: vm_method.c
===================================================================
--- vm_method.c	(revision 65919)
+++ vm_method.c	(revision 65920)
@@ -1089,7 +1089,14 @@ rb_export_method(VALUE klass, ID name, r https://github.com/ruby/ruby/blob/trunk/vm_method.c#L1089
 int
 rb_method_boundp(VALUE klass, ID id, int ex)
 {
-    const rb_method_entry_t *me = rb_method_entry_without_refinements(klass, id, NULL);
+    const rb_method_entry_t *me;
+
+    if (ex & BOUND_RESPONDS) {
+        me = method_entry_resolve_refinement(klass, id, TRUE, NULL);
+    }
+    else {
+        me = rb_method_entry_without_refinements(klass, id, NULL);
+    }
 
     if (me != 0) {
 	if ((ex & ~BOUND_RESPONDS) &&
Index: spec/ruby/core/module/refine_spec.rb
===================================================================
--- spec/ruby/core/module/refine_spec.rb	(revision 65919)
+++ spec/ruby/core/module/refine_spec.rb	(revision 65920)
@@ -524,21 +524,42 @@ describe "Module#refine" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/module/refine_spec.rb#L524
       }.should raise_error(NameError, /undefined method `foo'/)
     end
 
-    it "is not honored by Kernel#respond_to?" do
-      klass = Class.new
-      refinement = Module.new do
-        refine klass do
-          def foo; end
+    ruby_version_is "" ... "2.6" do
+      it "is not honored by Kernel#respond_to?" do
+        klass = Class.new
+        refinement = Module.new do
+          refine klass do
+            def foo; end
+          end
+        end
+
+        result = nil
+        Module.new do
+          using refinement
+          result = klass.new.respond_to?(:foo)
         end
-      end
 
-      result = nil
-      Module.new do
-        using refinement
-        result = klass.new.respond_to?(:foo)
+        result.should == false
       end
+    end
 
-      result.should == false
+    ruby_version_is "2.6" do
+      it "is honored by Kernel#respond_to?" do
+        klass = Class.new
+        refinement = Module.new do
+          refine klass do
+            def foo; end
+          end
+        end
+
+        result = nil
+        Module.new do
+          using refinement
+          result = klass.new.respond_to?(:foo)
+        end
+
+        result.should == true
+      end
     end
   end
 
Index: NEWS
===================================================================
--- NEWS	(revision 65919)
+++ NEWS	(revision 65920)
@@ -20,6 +20,8 @@ sufficient information, see the ChangeLo https://github.com/ruby/ruby/blob/trunk/NEWS#L20
 
 * refinements takes place at Kernel#public_send.  [Feature #15326]
 
+* refinements takes place at Kernel#respond_to?.  [Feature #15327]
+
 * +else+ without +rescue+ now causes a syntax error.  [EXPERIMENTAL]
 
 * constant names may start with a non-ASCII capital letter. [Feature #13770]

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

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