On 6/22/07, hellfeuer / gmail.com <hellfeuer / gmail.com> wrote:
> hey
Hi!

> i have a question:
A nice one too. I learned quite a bit from it so thank you.

After reading up on procs, evals, and lambdas in the pickaxe book I
think I understand your question. This irb snippet is where I realised
exactly what was going on so maybe it will help you as well.

sjs@tuono% ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [x86_64-linux]
sjs@tuono% irb
irb(main):001:0> p = lambda {a=:a; binding}
=> #<Proc:0x00002b17e252f1a0@(irb):1>
irb(main):002:0> binding = p.call
=> #<Binding:0x2b17e252a2b8>
irb(main):003:0> eval "p a", binding
:a
=> nil
irb(main):004:0> p a
NameError: undefined local variable or method `a' for main:Object
        from (irb):4
irb(main):005:0> eval "b=:b", binding
=> :b
irb(main):006:0> b
NameError: undefined local variable or method `b' for main:Object
        from (irb):6

> 2) the second version should bind 'b' in the context of foo's binding
> therefore making it local to foo, so foo should still be able to access
> b, but it should not be visible in the top level.. yet the opposite
> behaviour is observed
>
> how come?? what am i missing here?

This is how you you would define b in a given context, local to that
context. I don't think you were misunderstanding the expected
behaviour, just the method of describing that behaviour.

sjs@tuono% irb
irb(main):001:0> p = lambda {a=:inside; binding}
=> #<Proc:0x00002b321f1aee48@(irb):1>
irb(main):002:0> binding = p.call
=> #<Binding:0x2b321f1a9c40>
irb(main):003:0> p a    # this is the same as: eval "p a"
NameError: undefined local variable or method `a' for main:Object
        from (irb):3
irb(main):004:0> eval "p a", binding
:inside
=> nil
irb(main):005:0> a = :outside
=> :outside
irb(main):006:0> b = a
=> :outside
irb(main):007:0> eval "c = a", binding
=> :inside
irb(main):008:0> c
NameError: undefined local variable or method `c' for main:Object
        from (irb):9
irb(main):009:0> eval "b = a", binding
=> :inside
irb(main):010:0> b
=> :inside

Hope this helps!

-- 
Sami Samhuri