From: hemant Date: 2006-10-19T11:43:39+09:00 Subject: Re: whats this lambda code doing? On 10/19/06, hemant wrote: > On 10/19/06, Morton Goldberg wrote: > > On Oct 18, 2006, at 9:12 PM, Ezra Zygmuntowicz wrote: > > > > > > > > On Oct 18, 2006, at 6:02 PM, David Vallner wrote: > > > > > >> Ezra Zygmuntowicz wrote: > > >>> klasses.inject(block) do |blk, klass| > > >>> lambda { klass.with_scope(scope_hash, &blk) } > > >>> end.call > > >>> > > >>> That outer block that ends on end returns the lambda made inside > > >>> of that > > >>> block. So end.call ends up being lambda { klass.with_scope > > >>> (scope_hash, > > >>> &blk) }.call > > >>> > > >> > > >> Hmm. Unless I'm very mistaken, it would also only call the lambda for > > >> the last object in klasses, generating several garbage (and > > >> relatively > > >> expensive) lambdas. Boggle. Am I missing something? > > >> > > >> David Vallner > > > > > > It looks like it would only call the last lambda but it does call > > > all of them. Here is a simplification of whats happening. > > > > > > irb(main):065:0> def scoper(klasses=[:foo, :bar, :baz], &block) > > > irb(main):066:1> klasses.inject(block) do |blk, klass| > > > irb(main):067:2* lambda { puts klass; blk.call} > > > irb(main):068:2> end.call > > > irb(main):069:1> end > > > => nil > > > irb(main):070:0* scoper { puts '&block called' } > > > baz > > > bar > > > foo > > > &block called > > > => nil > > > irb(main):071:0> > > > > I think I'm still confused by this. I infer from your post that the > > lambdas get nested by the inject (at the position of 'blk') with the > > first one innermost, which is why they get called in reverse order > > (and 'block' last of all). Is that right? > > > > Regards, Morton > > > > Ok..i get it now..Thanks for the nice explanation Ezra. -- There was only one Road; that it was like a great river: its springs were at every doorstep, and every path was its tributary.