Marcin Raczkowski
11/13/2007 8:22:00 PM
Vasco Andrade e Silva wrote:
> Hi,
>
> def f_ok(node)
> func = lambda {|n| p(:in_lambda, n); n }
> p node, func.call(node[:xpto]), node
> end
>
>> f_ok({:xpto => "ohoh"})
> :in_lambda
> "ohoh"
> {:xpto=>"ohoh"}
> "ohoh"
> {:xpto=>"ohoh"}
>
> def f_ko(node)
> func = lambda {|node| p(:in_lambda, node); node }
> p node, func.call(node[:xpto]), node
> end
>
>> f_ko({:xpto => "ohoh"})
> :in_lambda
> "ohoh"
> {:xpto=>"ohoh"}
> "ohoh"
> "ohoh" # ohoh... should be {:xpto=>"ohoh"}, or not??
>
> I can't get any kind of explanation for this. Does anybody have one?
> Why "node" in lambda isn't shadowed correctly (as i expected, at least)?
> Well "node" is shadowed correctly but seems to be producing side
> effects...
>
> Thanks,
> Vasco Andrade e Silva
I remember someone mentioned that it's possible to do some strange
things in lambda (modyfing variable passed beeing one of them), but
there's nothing in this funcition or lambda that would modify "node", i
think there might be a scope problem that makes node variable in
function beeing overvriten when part of node is in lambda
irb(main):014:0> def f_ko(node)
irb(main):015:1> func = lambda {|node| p(:in_lambda, node); node }
irb(main):016:1> p node, func.call("bleh"), node
irb(main):017:1> end
=> nil
irb(main):018:0> f_ko({:xpto => "ohoh"})
:in_lambda
"bleh"
{:xpto=>"ohoh"}
"bleh"
"bleh"
=> nil
this seems to confirm that using |node| will overwrite node in function,
i'm not sure if it's intended scope behaviour tho