From: Kent Dahl Date: 2004-06-05T19:18:42+09:00 Subject: Re: Check assertion in Ruby Michael Neumann wrote: > Or even better combining both approaches: > > def check(a=nil, &block) > raise "check failed" unless (block ? block.call : a) > end > > Wouldn't that make sense to add to module Kernel? Or maybe named as > assert? I don't like unnecessary object construction, such as that Proc-wrapping. def check(a=nil) raise "check failed" unless (block_given? ? yield(a) : a) end I'd also yield the given argument if both a block and an argument are supplied. It is nice to avoid an extra assignment line when doing larger checks: check( my.insanely.long.chained.method.call(:with,:lots,:of,*arguments) ){|i| (i > 64 && i < 91) || (i > 96 && i < 123) } IMHO -- (\[ Kent Dahl ]/)_ _~_ _____[ http://www.pvv.org/~kentda/ ]_____/~ ))\_student_/(( \__d L b__/ Master of Science in Technology ) ( \__\_�|�_/__/ ) _) Industrial economics and technology management ( \____/_�_\____/ (____engineering.discipline_=_Computer::Technology___)