From: Robert Dober Date: 2006-05-10T01:23:25+09:00 Subject: Re: code snippet: can it be done better/shorter? On 5/9/06, Jacob Fugal wrote: > On 5/9/06, Krekna Mektek wrote: > > A bit late, but I was reading this post again, but your code is > > actually not the same, if I understood you right. > > > > because from is assigned "Unknown" if it's an empty string. > > And from || "Unknown" results in true if from is defined, so the value > > "Unknown" is not assigned to it. Right? > > The value "Unknown" will not be assigned to from, but it doesn't need > to be. An expression of the form 'a || b' is equivalent to this: > > a ? a : b Which is unfortunately not true because of possible side effects, in our case however there are none and it was a *nice way* to explain things, but watch out for method calls. > > or more verbosely: > > if a then a else b end > some as above but clearer. This is an interesting case for forcing () on method calls I like the ruby way but the potential problem with side effects becomes so much clearer in case of a() ? a() : whatever which still is perfect ruby code :) Cheers Robert -- Deux choses sont infinies : l'univers et la b�tise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein