Hi all,
One of the coolest things for me about learning ruby is that it’s the
first language I’ve learned in a decade where I actually had to learn a
new language. Once you know C/C++, you can pick up Java, PHP, perl,
Python, and even VB without really having to learn new grammar… you
just learn where the braces and dollar signs go. One of the joys of
ruby is that when I find myself using a klunky grammar construct, there
is often an elegant solution if I will just surrender my C++ thinking
style.
That paragraph could spark a thread of its own, but in this case, its
lead-up for a simpler question. Which do you find more readable:
this version
if !correct
handle_error
do_some_other_thing
end
or this version
unless correct
handle_error
do_some_other_thing
end
When dealing with single-lines, I find the inverted construct to be MUCH
more readable:
handle_error unless correct
but when I have to make a block, my C++ instincts really prefer the “if
!” version.
I’m just throwing this out here… what do you prefer, and why? Do you
find the “unless x” construct as (or more) readable than the “if !x”
construct? When I see a block beginning with unless, I really have to
stop reading and tease out the logic. Is this a good instinct thing, or
is it simply me lacking fluency in a common ruby idiom?
Hmm, I just discovered this syntax, and for small blocks, I prefer it
greatly:
( handle_error; do_some_other_thing ) unless correct
Though I worry that ( …, … ) syntax may have some side effects I’m
not expecting. That’s list declaration syntax, isn’t it? So using it
as a block might bend POLS…
-dB