From 83c6a1ef454c51ad1c0ca58e8a95fd67a033f710 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Fri, 30 Aug 2019 11:01:25 +0900 Subject: 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 ``` --- NEWS | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'NEWS') diff --git a/NEWS b/NEWS index 61f3fc8783..34a94763c5 100644 --- a/NEWS +++ b/NEWS @@ -160,6 +160,34 @@ Time:: * Added Time#floor method. [Feature #15653] +UnboundMethod:: + + New methods:: + + * Added UnboundMethod#bind_call method. [Feature #15955] + +`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. + + class Foo + def add_1(x) + x + 1 + end + end + class Bar < Foo + def add_1 # 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 + + $LOAD_PATH:: New method:: -- cgit v1.2.3