From: Gavin Sinclair Date: 2008-08-15T23:12:07+09:00 Subject: Re: Best practice for set-and-test idiom? On Aug 15, 3:51 am, Kevin Nolan wrote: > >      unless (ar = Model.find(...)).nil? >         val = ar.attribute >      else >         val = DEFAULT > >    or the equivalent expression. > > It seems to me that this is very unRuby-like and I don't see any similar > usages in any of the code-bases in which I have looked. > > Will someone please provide a counter-example? Does the Model.find(...) need to be buried in a test condition? If it's retrieving worthwhile data, it's worth a line of its own. model = Model.find(...) val = (model && model.attribute) || DEFAULT The second line could be called idiomatic Ruby. Whether it appeals to you or not, I can't predict :) For situations where more complex processing is required before setting the variable, I am a _big_ fan of these: val = if condition then processing processing value1 else processing value2 end val = case x when 1: ... when 2: ... ... end Cheers, Gavin