Ruby - standard operators are overloaded. Perl - they are not
Archive - Originally posted on "The Horse's Mouth" - 2012-06-09 15:29:43 - Graham Ellis
Ruby has been described to me as "What Perl 5.5 should have been", but that statement is a severe dis-service to Ruby, and to Perl. Ruby is a new language, and if you're moving into Ruby from Perl, you'll do best not to assume even the basics from Perl. A lot has been learend from Perl, for sure, but that does not mean it has been copied. And the differences start with the fact that in Ruby, everything is an object. With Ruby that goes further too; opearators are really just methods running on the object that's to their left.
munchkin:lruby grahamellis$ ruby nother
How warm is is today (C) 34
306
34
34
34
34
34
34
34
34
34
306.0
etc
The multiplication method differs ... in Ruby, if I multiply an integer by an integer, I get an integer result. If I multiply a floating point number by an integer, I get a floating point result. And if I multiply a string by an integer, I duplicate that string the number of times given by the integer.
In Perl, the * operator is always numeric and the data is silently converted from a string if necessary, and the x operator provides string replication - converting a number into a string ahead of time if it needs to do so.
The code used above is included in a complete example from the private Ruby Programming Course which I ran last week in Liverpool. Full example source [here]. Picture accompanyng article also from Liverpool - sort of appropriate as I've been examining code in detail in this article.