From: Kevin Nolan Date: 2008-08-15T04:39:20+09:00 Subject: Re: Best practice for set-and-test idiom? Tim Hunter wrote: > Kevin Nolan wrote: > (snip) >> or the compact version: >> >> val = (ptr = fcn_returning_ptr(...)) ? ptr->ref : DEFAULT >> >> Which, when I translate to Ruby yields (ActiveRecord example): >> >> 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? > > Ruby has the ternary ?: operator, too: > > val = (ar = Model.find(...)) ? ar.attribute : DEFAULT Tim, I know -- I just omitted it for the sake of brevity. Do you have any thoughts about a better, or my ruby-like technique? -- Posted via http://www.ruby-forum.com/.