[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Any way to evaluate blocks using an arbitrary Binding?

Kenneth McDonald

8/30/2007 10:59:00 PM

Using Kernel.eval, I can evaluate a Ruby string under an arbitrary
variable Binding object. However, while I can obtain from a Proc the
Binding it uses when it is evaluated, there does not seem to be any way
of evaluating a Proc with a different Binding. Is this possible in some way?

Thanks,
Ken

4 Answers

Robert Klemme

8/31/2007 6:03:00 AM

0

On 31.08.2007 00:58, Kenneth McDonald wrote:
> Using Kernel.eval, I can evaluate a Ruby string under an arbitrary
> variable Binding object. However, while I can obtain from a Proc the
> Binding it uses when it is evaluated, there does not seem to be any way
> of evaluating a Proc with a different Binding. Is this possible in some
> way?

Sort of: you can use instance_eval and class_eval with a block. While
only "self" will be rebound this is sufficient in many cases.

Kind regards

robert

Kenneth McDonald

8/31/2007 6:57:00 AM

0

Robert,

I was already aware of instance_eval (have to look up class_eval) used
in this manner (possibly from another of your very useful postings), but
had wondered if there was a more general way of affecting the execution
environment. Oh well.

Thanks,
Ken



Robert Klemme wrote:
> On 31.08.2007 00:58, Kenneth McDonald wrote:
>> Using Kernel.eval, I can evaluate a Ruby string under an arbitrary
>> variable Binding object. However, while I can obtain from a Proc the
>> Binding it uses when it is evaluated, there does not seem to be any
>> way of evaluating a Proc with a different Binding. Is this possible
>> in some way?
>
> Sort of: you can use instance_eval and class_eval with a block. While
> only "self" will be rebound this is sufficient in many cases.
>
> Kind regards
>
> robert
>
>


Joel VanderWerf

8/31/2007 7:08:00 AM

0

Kenneth McDonald wrote:
> Robert,
>
> I was already aware of instance_eval (have to look up class_eval) used
> in this manner (possibly from another of your very useful postings), but
> had wondered if there was a more general way of affecting the execution
> environment. Oh well.

You can change the binding one variable at a time:

def foo
x = 1
proc {puts "x = #{x}"}
end

pr = foo

p eval("x", pr)
eval("x=2", pr)
p eval("x", pr)

pr.call

__END__

Output:

1
2
x = 2

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Desertphile

1/28/2011 5:10:00 PM

0

On Wed, 26 Jan 2011 14:46:52 -0800 (PST), Grendel
<wsthomas@bellsouth.net> wrote:

> Anthropogenic Global Warming is a theory.

Exactly! Thank you for admitting that. What made you change your
mind?


--
http://deser...
Desertphile's Desert Soliloquy. WARNING: view with plenty of water
"Why aren't resurrections from the dead noteworthy?" -- Jim Rutz