On 2/16/06, Phil Tomson <ptkwt / aracnet.com> wrote:
> What I'm trying to do probably isn't possible, but maybe someone knows some
> deep magic.
>
> I'd like something like:
>
>   def runit &b
>     a = 42
>     pnew = Proc.new &b, binding
>     pnew.call
>   end
>
>   runit { puts "a is: #{a}" }
>
>   #would print:
>   a is: 42
>
> Or course, Proc.new currently doesn't take a Binding object second argument.
> What I'm wondering is if there is any way to 'rebind' or 'recontextualize' a
> block?

This is interesting. For a while you had me thinking the Proc.new {} should
rebind, but I couldn't get it to. Seems that the binding is based upon where the
block is defined inside the file.

This is the closest I could get:

  def runit(b)
    a = 42
    pnew = lambda { eval(b,binding) }
    pnew.call
  end

  runit %q{ puts "a is: #{a}" }



--
Jim Freeze