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

ruby-changes:58444

From: Jeremy <ko1@a...>
Date: Sat, 26 Oct 2019 09:29:15 +0900 (JST)
Subject: [ruby-changes:58444] d00551a7bb (master): [ruby/forwardable] Make def_{instance, single}_delegators skip :__send__ and :__id__

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

From d00551a7bb1d5dbabb0a8c95d6bd2d9d4e86934a Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Thu, 15 Aug 2019 15:21:55 -0700
Subject: [ruby/forwardable] Make def_{instance,single}_delegators skip
 :__send__ and :__id__

Previously, __send__ and __id__ were skipped if provided as strings,
but not skipped if provided as symbols.

Fixes Ruby Bug 8855.

https://github.com/ruby/forwardable/commit/2e61c8c66c

diff --git a/lib/forwardable.rb b/lib/forwardable.rb
index 7f21923..3ca8a25 100644
--- a/lib/forwardable.rb
+++ b/lib/forwardable.rb
@@ -149,9 +149,8 @@ module Forwardable https://github.com/ruby/ruby/blob/trunk/lib/forwardable.rb#L149
   #   def_delegator :@records, :map
   #
   def def_instance_delegators(accessor, *methods)
-    methods.delete("__send__")
-    methods.delete("__id__")
-    for method in methods
+    methods.each do |method|
+      next if method.to_s == "__send__" || method.to_s == "__id__"
       def_instance_delegator(accessor, method)
     end
   end
@@ -286,9 +285,8 @@ module SingleForwardable https://github.com/ruby/ruby/blob/trunk/lib/forwardable.rb#L285
   #   def_delegator :@records, :map
   #
   def def_single_delegators(accessor, *methods)
-    methods.delete("__send__")
-    methods.delete("__id__")
-    for method in methods
+    methods.each do |method|
+      next if method.to_s == "__send__" || method.to_s == "__id__"
       def_single_delegator(accessor, method)
     end
   end
diff --git a/test/test_forwardable.rb b/test/test_forwardable.rb
index 0a90d3a..2c91d6f 100644
--- a/test/test_forwardable.rb
+++ b/test/test_forwardable.rb
@@ -107,6 +107,18 @@ class TestForwardable < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_forwardable.rb#L107
     end
   end
 
+  def test_def_instance_delegators_send_id
+    %i[def_delegators def_instance_delegators].each do |m|
+      cls = forwardable_class do
+        attr_reader :receiver
+        __send__ m, :@receiver, :__send__, :__id__
+      end
+
+      assert_not_equal cls.new.__id__, cls.new.receiver.__id__
+      assert_not_equal cls.new.__send__(:__id__), cls.new.receiver.__send__(:__id__)
+    end
+  end
+
   def test_instance_delegate
     %i[delegate instance_delegate].each do |m|
       cls = forwardable_class do
@@ -215,6 +227,18 @@ class TestForwardable < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_forwardable.rb#L227
     end
   end
 
+  def test_obj_single_delegators_send_id
+    %i[def_delegators def_single_delegators].each do |m|
+      obj = single_forwardable_object do
+        singleton_class.attr_reader :receiver
+        __send__ m, :@receiver, :__send__, :__id__
+      end
+
+      assert_not_equal obj.__id__, obj.receiver.__id__
+      assert_not_equal obj.__send__(:__id__), obj.receiver.__send__(:__id__)
+    end
+  end
+
   def test_obj_single_delegate
     %i[delegate single_delegate].each do |m|
       obj = single_forwardable_object do
-- 
cgit v0.10.2


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

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