Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Any way to evaluate blocks using an arbitrary Binding?

Reply
Thread Tools

Any way to evaluate blocks using an arbitrary Binding?

 
 
Kenneth McDonald
Guest
Posts: n/a
 
      08-30-2007
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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-31-2007
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
 
Reply With Quote
 
 
 
 
Kenneth McDonald
Guest
Posts: n/a
 
      08-31-2007
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
>
>



 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      08-31-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
"Building Blocks" are "Application Blocks" Arjen ASP .Net 3 02-27-2005 01:06 AM
calling an arbitrary function w/ arbitrary arguments Honestmath C++ 5 12-13-2004 06:18 AM
procs/blocks - blocks with procs, blocks with blocks? matt Ruby 1 08-06-2004 01:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57