summaryrefslogtreecommitdiff
path: root/test/ruby/test_method.rb
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2019-08-30 11:01:25 +0900
committerYusuke Endoh <mame@ruby-lang.org>2019-08-30 11:13:00 +0900
commit83c6a1ef454c51ad1c0ca58e8a95fd67a033f710 (patch)
tree1c54ce712e993cf3ddfe9577345ca0b70b7c993d /test/ruby/test_method.rb
parent500149709b92ccb36396589a0c39afb3ff77bfb6 (diff)
proc.c: Add UnboundMethod#bind_call
`umethod.bind_call(obj, ...)` is semantically equivalent to `umethod.bind(obj).call(...)`. This idiom is used in some libraries to call a method that is overridden. The added method does the same without allocation of intermediate Method object. [Feature #15955] ``` class Foo def add_1(x) x + 1 end end class Bar < Foo def add_1(x) # override x + 2 end end obj = Bar.new p obj.add_1(1) #=> 3 p Foo.instance_method(:add_1).bind(obj).call(1) #=> 2 p Foo.instance_method(:add_1).bind_call(obj, 1) #=> 2 ```
Diffstat (limited to 'test/ruby/test_method.rb')
-rw-r--r--test/ruby/test_method.rb9
1 files changed, 9 insertions, 0 deletions
diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb
index acaf43e46c..1d59ddb594 100644
--- a/test/ruby/test_method.rb
+++ b/test/ruby/test_method.rb
@@ -1140,4 +1140,13 @@ class TestMethod < Test::Unit::TestCase
assert_equal(m, o.:foo)
assert_nil(o.method(:foo))
end
+
+ def test_umethod_bind_call
+ foo = Base.instance_method(:foo)
+ assert_equal(:base, foo.bind_call(Base.new))
+ assert_equal(:base, foo.bind_call(Derived.new))
+
+ plus = Integer.instance_method(:+)
+ assert_equal(3, plus.bind_call(1, 2))
+ end
end