From: Brian Candler Date: 2004-09-29T18:18:28+09:00 Subject: Re: DRY ruby idiom > I will not that (in the original poster's terms), this is > > var = val || var or a = foo.value || a which more clearly shows the value (!) of this construct. Smart. Thinking about my own code, I tend to write var = val unless val.nil? which is even more verbose, but seems a bit clearer, and works for the case where val=false. However I end up assigning to a temporary variable 'val' in the case of a complex expression. Maybe I'll adopt your pattern instead, although I think it would leave some people scratching their heads at first. There is the more general case of if some.complex.expression ... do something with some.complex.expression end which I typically write as if (val = some.complex.expression) ... do something with val end This does suggest some other solutions: a = tmp if tmp = foo.value ## fails - first tmp is treated as method if tmp = foo.value; a = tmp; end (tmp = foo.value) && (a = tmp) Icky though. It might be nice if the value of the condition evaluated in an 'if', or the LHS of && or ?:, were somehow available to the body/RHS. I can't think how it would work nicely: foo.value && a = $# #yuk Regards, Brian.