Joe Ruby MUDCRAP-CE wrote:
> One thing that annoys me about Ruby is that casting variables is 
> required:
> 
> i = 5
> a = 'logan'
> 
> puts a + i.to_s
> 
> Or
> 
> a='2'
> b=3
> 
> puts a.to_i + b
> 
> Why can't (or can?) Ruby just handle 'puts a + i' or 'puts a+b'?

It does.  It just doesn't handle it the way you might prefer.  If Ruby 
did automatic coercion, then someone else would wonder why Ruby doesn't 
"correctly: handle the '+' operation.

Given this code:

a='2'
b=3

puts a+b

This is sending the message '+' to the String object referenced by 'a'. 
  Strings have a particular implementation of the '+' message, and it 
assumes that the arguments will be something that implements certain 
behavior.  (I.e., is sufficiently String-like ).

By default, the above code barfs with "... can't convert Fixnum into 
String (TypeError)"

You can teach the Integer class how to stringify itself, though:

class Integer
   def to_str
     self.to_s
   end
end

But note that there is an important side-effect: Integers mistakenly 
used in place of a String will now silently convert themselves, which 
may not be the behavior you (or users of your code) will always want.

Ruby has strong dynamic typing; objects need only respond to the set 
messages your code needs to send to them in order to be useful (AKA duck 
typing).  That's the dynamic part. But Ruby will not automagically alter 
the behavior of objects.  That's the strong part.

---
James Britt

"Blanket statements are over-rated"