From: Rick DeNatale Date: 2010-03-31T00:33:33+09:00 Subject: Re: coercing nil/obj to false/true On Tue, Mar 30, 2010 at 3:17 AM, Robert Klemme wrote: > 2010/3/30 Steve Howell : > >> Is there a way to avoid this idiom? >> >> def is_there_one(whatever) >>  !some_call_to_lib_that_returns_object_or_nil(whatever).nil? >> end > >> Finally, I can just have my method return obj or nil, and let the >> callers use it in boolean expressions, but my fear there is that I set >> the wrong expectation for the caller, if I later return a different >> object, when my intention is only to indicate whether a query succeeds >> or not.  (You guessed it, this is motivated by ActiveRecord, but it's >> really a Ruby question.) > > Your method's documentation and naming the method with a question mark > at the end will help avoid this.  I would be more concerned with > leaking internal information.  That's why people often do this in > order to force conversion to true / false: > > def is_there_one? whatever >  !! some_call_to_lib_that_returns_object_or_nil(whatever) > end I almost never find that I need to 'force' a 'true' boolean value in Ruby. I pretty much agree with Koz about this. http://www.therailsway.com/2007/8/1/dangers-of-cargo-culting -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale