So a co-worker sent me this link of “cool Ruby tricks“. After looking at them, I’ve come up with the Ruby equivalent of Perl’s TIMTOWTDI (There is more then one way to do it.)
JBYCDMYS – Just because you can, doesn’t mean you should. Seriously, anyone who thinks nested ternary operators (#15) is a good thing, or that:
(z ||= ) < < 'test'
(#7) is readable code really needs to get out more. And then there’s the suggestion of using the .. operator instead of > and < for checking ranges (#12) without warning it's about 20% slower. Don't get me wrong, I love Ruby- it's a fun, expressive and powerful language, but like all languages you can shoot yourself in the foot.
One thing about #12, is that it gets worse the larger the range is. The above test showing 20% slower was for the range of 1..1000. I then did another test to see if a random number was in the range of
0 >= x >= 2**32-1 or
and it took twice as long as the traditional method.