From: Ezra Zygmuntowicz Date: 2006-10-19T09:54:03+09:00 Subject: Re: whats this lambda code doing? On Oct 18, 2006, at 5:48 PM, hemant wrote: > I came across following code in typo's application.rb and I can't > understand the last part, > > > def with_blog_scoped_classes(klasses=[Content, Article, Comment, > Page, Trackback], &block) > default_id = this_blog.id > scope_hash = { :find => { :conditions => "blog_id = # > {default_id}"}, > :create => { :blog_id => default_id } } > klasses.inject(block) do |blk, klass| > lambda { klass.with_scope(scope_hash, &blk) } > end.call > end > > Method will be called with a block and will probably add some scope to > the respective methods of ActiveRecord classes. But whats the last > part? > I mean, end.call? 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 -- Ezra Zygmuntowicz -- Lead Rails Evangelist -- ez@engineyard.com -- Engine Yard, Serious Rails Hosting -- (866) 518-YARD (9273)