Alternative loops and conditionals in Ruby and Perl
Archive - Originally posted on "The Horse's Mouth" - 2010-07-28 07:33:49 - Graham Ellis
Ruby gets a lot from Perl - including its eclecticness in having a wide variety of alternative ways of doing similar things - and no more so than in loops and conditionals. Not only do you have an if but also an unless. Not only do you have a while but also an until. And not only can you write these as conditional blocks (with the conditional code after the condition) but also the other way round as conditional modifiers.
so if you don't like this:
if (n <= 4)
c = c + 3
end
try this:
c += 3 unless n > 4
Further examples [here] and something similar in Perl [here].
Is all this variety a good thing? If used with care, yes ... but if thrown about with gay abandon, leading to unmaintainable code, probably not ... which means that in both Ruby and Perl you really need to think about code quality and standards from day one. Or should I say from before day one ...