RCR 329: Proc.call_in or similar functionality to call a block in a g
Submitted by cryo (Sun Feb 26 20:12:59 UTC 2006)
Abstract
This is related to RCR 251, which notes that "a block version should also be available" or something (inexact quote). That RCR is accepted, but it's not clear if that includes that small part in parenthesis.
Either Binding could have an eval that accepted blocks (or Procs) as suggested, or, perhabs better wrt. argument passing, something like Proc.call_in.
Problem
I have some text representing ruby code, and I want to evaluate it many times, but with a different local variable setup. If that last bit wasn't needed, I would use eval to turn it into a Proc (by adding lambda {.. before and } after the text), and just .call it when needed. This, however, doesn't work when I need to call it in a different binding. For that, only eval works, which only takes a string as its input. This means reparsing each time.
Proposal
Something like a call_in method in Proc (or a better name), which worked like this: b = binding # b now points to some binding. ... p = lambda... # p is a Proc of some kind. .... p.call_in(b, args...) # Call p in the binding b, with arguments.
Analysis
We have .module_eval and .instance_eval already. Both take blocks. The latter also modifies the binding, by re-assigning self. As for Proc.call_in, there could of course be a problem that I didn't perceive, which prevents or makes implementation very difficult.
Implementation
Currently, the only thing that can be done is to use Kernel.eval. This doesn't work with Proc's though, so if your Proc isn't from eval'ed text, it can't be done at all.
Can you show us a concrete example of call_in usage?
-matz.
|
Strongly opposed |
0 |
Opposed |
0 |
Neutral |
0 |
In favor |
0 |
Strongly advocate |
0 |
|
RCRchive copyright © David Alan Black, 2003-2005.
Powered by .
-matz.