Jeremy Neander

== vs. eql vs. equals

It was suggested to me that it is common for Ruby developers to default to == when testing comparisons in their spec tests. This seemed odd to me, but it merited a little research in any case.

The operators act as follows

1.0 == 1
# => true

1.0.eql? 1
# => false

a = 1.0
b = 1.0
a.equals? a
# => true

a.equals? b
# => false

In testing, the operators differ only visually

As a rule of thumb: the longer the operator, the more restrictive the test it performs.

It may be true that programmers tend to use the == operator. For me, the information being compared - not personal style or preference - should be the deciding factor for the choice between one operator or another. After all, I want to be absolutely certain that my tests are passing for the right reasons.