From: Jon Bauman Date: 2006-04-13T00:10:27+09:00 Subject: Re: Style question: if !x vs. unless x I don't think you can actually use elsif with an unless block: $ irb --simple-prompt >> unless true >> elsif true >> end SyntaxError: compile error (irb):2: parse error, unexpected kELSIF elsif true ^ from (irb):3 >> if true >> elsif true >> end => nil Personally, I use unless any time my primary test is for a negated condition (and I don't need elsif). It takes a little while to get used to perhaps, but if you think about it as meaning "if not" (which is what unless actually means) it works. The think I like about using "unless x" as opposed to "if !x" or "if not x" is that there is no precedence to think about, it's totally clear that it negates the entire condition. zdennis wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Logan Capaldo wrote: >> >> >> unless condition >> ... >> elsif othercondition >> ... >> else >> ... >> end >> > > > I mean what Logan said plus the second half of what Dave said. I do not > like the following, and I find it a) sometimes confusing > to read and b) my brain has to think twice when reading, which is twice > to much: > > unless condition > ... > elsif > ... > else > ... > end > > unless cond1 and cond2 and cond3 > ... > end > > unless (cond1 and cond2) or cond3 > ... > end > > do_something unless cond1 and cond2 > > In regards to the first half of Dave's email: > >> >> if x >> do_something >> end unless y >> >> I do like this syntax, though: >> >> do_something if condition unless exception >> > > I have never thought of chaining conditionals like that, although the > idea is very cool... and I find it readable in the case of > error handling. (I wont say exception handling because IMO the two > differ and exceptions should be handled by a different > mechanism then what error conditions should be handled under, the whole > 90/10 rule applies of course.) =) > > Zach > > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.1 (GNU/Linux) > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org > > iD8DBQFEPM/LMyx0fW1d8G0RAvJgAJwKfktuwgUbcI6iZ+Nd0mVzqxoq5gCeJhoX > f7Ygn98m+JdYMPB5ZkrvL4c= > =h6ey > -----END PGP SIGNATURE----- -- Posted via http://www.ruby-forum.com/.