In article <CEENJIKJHPPGICBNAFMIGECADCAA.mchahn / facelink.com>, "Mark Hahn"
<mchahn / facelink.com> wrote:


> The reason I lost the argument was that they claimed it would cause
> hard-to-find bugs if someone meant: "1".to_int + 1.  They claimed it was
> ambiguous.  To me it is obvious that "1"+1 means a string and 1+"1"
> means an integer (because of message passing).

You can make this work the way you want (although, you'd have to stick
this code in a module and "require" it in all your code).  Note that I'm
not known for writing the simplest version of things, so someone else
will probably have to clean it up for me (and I've only done the 1 + "1"
case for Fixnum; it would probably have to be coded more generally to be
useful) ...

class String
    alias old_plus +
 
    def +(x)
        if x.type == String
            old_plus x
        else
            old_plus x.to_s
        end
    end
end
 
 
class Fixnum
    alias old_plus +
 
    def +(x)
        if x.type == String
            old_plus x.to_i
        else
            old_plus x
        end
    end
end
 
puts "1" + 1 # => "11"
puts 1 + "1" # => 2