From: Michael Neumann Date: 2004-06-05T18:37:43+09:00 Subject: Re: Check assertion in Ruby On Sat, Jun 05, 2004 at 08:50:22AM +0900, David A. Black wrote: > Hi -- > > On Sat, 5 Jun 2004, Michael Neumann wrote: > > > On Sat, Jun 05, 2004 at 03:34:47AM +0900, Edgardo Hames wrote: > > > Hi, everybody. I would like to use an assert like function to test the > > > number of rows returned by a query. Something like Eiffel's > > > > > > check result.num_rows = 1 > > > > > > What's the best way to do that in Ruby? > > > > def check > > raise "check failed" unless yield > > end > > > > > > check { result.num_rows == 1 } > > I guess you could even do: > > def check(a) > raise "check failed" unless a > end 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 quite often need an assert method... Regards, Michael