From: Chris Hulan Date: 2008-01-31T23:54:57+09:00 Subject: Re: can ik make this more beautifull? On Jan 31, 9:14 am, Mike Berrow wrote: > That is apparently a matter of opinion. > I find the single line alternative clear, concise and quite beautiful. > It reads just like a sentence for me. > > The 9-line alternative is annoyingly verbose and just burns vertical > space. > That style has me paging up and down or reaching for the scroll bar more > often. > Having to do that does not make the code easier to read in my opinion. > > -- Mike Berrow > > --------------------------- > > > > Peter Hickman wrote: > > Jari Williamsson wrote: > >>> if condition3 > >> A case expression is probably the most clear alternative. The shortest > >> is perhaps something like: > > >> x = condition1 ? 1 : condition2 ? 2 : 3 > > >> Best regards, > > >> Jari Williamsson > > > Short it may be but this sort of code can hardly be called beautiful, I > > have had to maintain crap like this before and the few keystrokes the > > author saved was made up for 1000 times when someone updated it and > > things didn't 'quite' work how they thought. Ruby allows simplicity and > > clarity, this however is heading into obfuscation. > > > The better solution is to use the elsif keyword to flatten the > > indentation: > > > if (temp == 'cat') > > x = 1 > > elsif(temp == 'dog') > > x = 2 > > elsif(temp == 'banana') > > x = 3 > > else > > x = nil > > end > > -- > Posted viahttp://www.ruby-forum.com/. Since your making a choice, a hash seems like it might be a tidy option: answers = {condition1=>1, condition2=>2, condition3=>3} x = answers[condition] As for the oneline version, I always find it particulary confusing in Ruby as you can have ? at the end of a method or it can be used in front a character to get the characters ASCII code. And nesting just multiplies the problemn, IMHO cheers