[ruby-core:105871] [Ruby master Feature#18276] `Proc#bind_call(obj)` same as `obj.instance_exec(..., &proc_obj)`
From:
"Eregon (Benoit Daloze)" <noreply@...>
Date:
2021-10-29 20:29:07 UTC
List:
ruby-core #105871
Issue #18276 has been updated by Eregon (Benoit Daloze).
I don't like the idea to alter the semantics of Proc methods just for Ractor though (also it costs extra checks on every Proc#call !).
The best and cleanest solution is IMHO to raise for `Ractor.make_shareable(someProc)` if the Proc `self` is not shareable.
See https://bugs.ruby-lang.org/issues/18243#note-5 for more details on that idea.
Then `Proc#bind_call` is simply not needed for Ractor.
I don't mind adding `Proc#bind_call` for other purposes though.
Changing the `self` is typically best avoided except for some cases in DSLs, as it breaks what methods the block can call in its lexical context.
----------------------------------------
Feature #18276: `Proc#bind_call(obj)` same as `obj.instance_exec(..., &proc_obj)`
https://bugs.ruby-lang.org/issues/18276#change-94409
* Author: ko1 (Koichi Sasada)
* Status: Open
* Priority: Normal
----------------------------------------
`Proc#bind_call(obj)` same as `obj.instance_exec(..., &proc_obj)`
```ruby
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`?
```ruby
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.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>