From: "Dan0042 (Daniel DeLorme)" Date: 2021-11-08T15:45:50+00:00 Subject: [ruby-core:105968] [Ruby master Feature#18276] `Proc#bind_call(obj)` same as `obj.instance_exec(..., &proc_obj)` Issue #18276 has been updated by Dan0042 (Daniel DeLorme). Eregon (Benoit Daloze) wrote in #note-10: > I'd assume on that object's singleton class like `instance_exec`, but I guess it's not the only possibility. I would assume the same thing; it would be pretty strange if this defined a `foo` method on class C. But it's interesting how instance_eval/instance_exec automatically creates a singleton_class, I wasn't aware of that before. ```ruby p ObjectSpace.each_object(Class).count #=> 363 Object.new.instance_eval{ } p ObjectSpace.each_object(Class).count #=> 364 Object.new.instance_exec{ } p ObjectSpace.each_object(Class).count #=> 365 ``` It looks like the singleton_class is eagerly created just in case the eval'd block contains a `def`. But it shouldn't be too hard to fix that to lazily create the singleton_class when needed. ---------------------------------------- Feature #18276: `Proc#bind_call(obj)` same as `obj.instance_exec(..., &proc_obj)` https://bugs.ruby-lang.org/issues/18276#change-94515 * 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: