From: dblack@... Date: 2007-03-01T20:48:18+09:00 Subject: Re: default passed block for method Hi -- On Thu, 1 Mar 2007, Niko wrote: > > Hi all, > > I'm trying to define the missing block given in the context of > the method to avoid the block_given? test on each iteration for > yield call. > > def self.list_of_relationships(relations, source) > # missing_block_that_yield_would_call = lambda { || true } > unless block_given? > relations.map do |relation| > if yield(peer = relation.send(source)) > [peer.display_name, peer.id] > end > end.compact > end You would want to do: def self.list(relations,source,&block) block ||= lambda { true } # no need for empty || relations.map do |relation| if block.call(peer = .... etc. You'd have to do some benchmarks to find out whether the slowdown from call is worse than the slowdown from block_given?. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)