Search code examples
rubyjrubyrubymine

Does Ruby auto-promote numeric values for comparison?


Why on earth does comparing a float value of 1.0, to an integer value of 1, return true?

puts '1.0'.to_i
puts '1.0'.to_i == 1.0 #so 1 == 1.0 is true?
puts 1.0 == 1 #wtf?

Does Ruby only read the first part of the floatin point value and then short circuit? Wuld someone be able to explain with a link to some documentation please? I have flipped through the API but I don't even know what to look for in this case...


Solution

  • In Ruby, == is a method. That means to understand it you need to look at the specific class calling it.

    1 == 1.0
    

    The caller is 1, a Fixnum. So you need to look at Fixnum#==.

    1.0 == 1
    

    The caller is 1.0, a Float. So you need to look at Float#==.

    A surprising result of this is that == is not necessarily symmetric: a == b and b == a could call completely different methods and return completely different results. In this case though, both == methods end up calling the C function rb_integer_float_eq which converts both operands to the same data type before comparing them.