Skip to content

Commit 2e61c8c

Browse files
committed
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.
1 parent 9f899dc commit 2e61c8c

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/forwardable.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ def instance_delegate(hash)
148148
# def_delegator :@records, :map
149149
#
150150
def def_instance_delegators(accessor, *methods)
151-
methods.delete("__send__")
152-
methods.delete("__id__")
153-
for method in methods
151+
methods.each do |method|
152+
next if method.to_s == "__send__" || method.to_s == "__id__"
154153
def_instance_delegator(accessor, method)
155154
end
156155
end
@@ -280,9 +279,8 @@ def single_delegate(hash)
280279
# def_delegator :@records, :map
281280
#
282281
def def_single_delegators(accessor, *methods)
283-
methods.delete("__send__")
284-
methods.delete("__id__")
285-
for method in methods
282+
methods.each do |method|
283+
next if method.to_s == "__send__" || method.to_s == "__id__"
286284
def_single_delegator(accessor, method)
287285
end
288286
end

test/test_forwardable.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ def test_def_instance_delegators_using_block_method_as_receiver
9696
end
9797
end
9898

99+
def test_def_instance_delegators_send_id
100+
%i[def_delegators def_instance_delegators].each do |m|
101+
cls = forwardable_class do
102+
attr_reader :receiver
103+
__send__ m, :@receiver, :__send__, :__id__
104+
end
105+
106+
assert_not_equal cls.new.__id__, cls.new.receiver.__id__
107+
assert_not_equal cls.new.__send__(:__id__), cls.new.receiver.__send__(:__id__)
108+
end
109+
end
110+
99111
def test_instance_delegate
100112
%i[delegate instance_delegate].each do |m|
101113
cls = forwardable_class do
@@ -204,6 +216,18 @@ def test_obj_single_delegators
204216
end
205217
end
206218

219+
def test_obj_single_delegators_send_id
220+
%i[def_delegators def_single_delegators].each do |m|
221+
obj = single_forwardable_object do
222+
singleton_class.attr_reader :receiver
223+
__send__ m, :@receiver, :__send__, :__id__
224+
end
225+
226+
assert_not_equal obj.__id__, obj.receiver.__id__
227+
assert_not_equal obj.__send__(:__id__), obj.receiver.__send__(:__id__)
228+
end
229+
end
230+
207231
def test_obj_single_delegate
208232
%i[delegate single_delegate].each do |m|
209233
obj = single_forwardable_object do

0 commit comments

Comments
 (0)