Gavin Sinclair
8/15/2008 2:12:00 PM
On Aug 15, 3:51 am, Kevin Nolan <kpno...@insgraph.com> 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