From: furufuru@... (Ryo Furue) Date: 2001-11-20T20:51:53+09:00 Subject: [ruby-talk:25907] String#== : Why not error with different type? Hi there, I think it would be nice if String#== raised error if the argument isn't a String. What was the reason why the designer(s) of the String class decided the equality method should return false as a = "2" b = 2 a == b # => false ? I wish the last line raised an error.... I have two reasons why I think comparison of objects with different types *should* be an error. 1) We naturally think that 'a == b' => true means that objects a and b have the same value. Then, most of us intuitvely think that 'a == b' => false means that they have different values. But, the actual implementation of String#== confuses us because 'a == b' => false can mean two very different things: that the values are different or that a and b are of different types. Of course, you could argue that 'a == b' => true means that a and b are of the same type and have the same value while 'a == b' => false means that 'a == b' is not true. Logical. But, it's more natural and intuitive to say that a Sting and a Fixnum can't be compared. 2) Not only it's more natural but also it's less prone to errors. In fact, I spent hard time to track down a bug which resulted from this: # at one place a = file0.gets #... # at another b = file1.gets.to_i #... # later if (a == b) .... ## BUG!!! The datafiles contained a bunch of integers. But, since my purpose was to merge two datasets into one, I actually didn't need to convert strings to integers (but, it was equally OK if I converted). I didn't take care much because I knew Ruby is usually strict in type mismatch. (3 + "4" raises an error, for example.) What do you think? Thank you, Ryo