From: Brian Candler Date: 2007-03-01T20:55:03+09:00 Subject: Re: default passed block for method On Thu, Mar 01, 2007 at 08:41:19PM +0900, Niko wrote: > 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 def foo(*args, &blk) blk ||= lambda { true } blk.call(*args) end p foo("123") { |x| "Look: #{x}" } p foo("456") It looks like you can't provide a default value for the block in the direct way: def foo(*args, &blk = lambda { true } ) # syntax error, unexpected '=', expecting ')' end HTH, Brian.