These two operator in Ruby look similar but can have different results when comparing Numeric operators.

1.0 == 1 #=> true
1.eql? 1 #=> false

As stated in the docs only override the ‘==’ if needed.

:)

One thought on “Ruby Tips #363 – Eql? versus == with Numerics

  1. Fred says:

    I think you meant to write either

    1.eql? 1.0 #=> false
    1.0.eql? 1 #=> false

    because

    1.eql? 1 #=> true

    The reason is that numeric types perform type conversion across == but not eql?

Leave a Reply

Your email address will not be published. Required fields are marked *