Project

General

Profile

Actions

Feature #18276

closed

`Proc#bind_call(obj)` same as `obj.instance_exec(..., &proc_obj)`

Added by ko1 (Koichi Sasada) over 2 years ago. Updated over 2 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:105853]

Description

Proc#bind_call(obj) same as obj.instance_exec(..., &proc_obj)

proc_obj = proc{|params...| ...}
obj.instance_exec(params..., &proc_obj)

is frequent pattern.

$ gem-codesearch 'instance_exec.+\&' | wc -l
9558

How about to introduce new method Proc#bind_call?

class Proc
  def bind_call obj, *args
    obj.instance_exec(*args, &self)
  end
end

pr = ->{ p self }
pr.bind_call("hello") #=> "hello"
pr.bind_call(nil)     #=> nil

It is similar to UnboundMethod#bind_call.


My motivation;

I want to solve shareable Proc's issue https://bugs.ruby-lang.org/issues/18243 and one idea is to prohibit Proc#call for shareable Proc's, but allow obj.instance_exec(&pr). To make shortcut, I want to introduce Proc#bind_call.

UnboundProc is another idea, but I'm not sure it is good idea...

Anyway, we found that there are many usage of instance_exec(&proc_obj), so Proc#bind_call is useful not for Ractors.


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #18243: Ractor.make_shareable does not freeze the receiver of a Proc but allows accessing ivars of itClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0