Category :

Published on :Nov 20, 2015

I don't remember since when did I pick up habit of preferring eql? over ==. But today I found out when should it be actually preferred and when it is ok to use ==.

Numbers

2.2.0 :001 > 1 == 1
 => true
2.2.0 :002 > 1 == 1.0
 => true
2.2.0 :003 > 1.eql?1
 => true
2.2.0 :004 > 1.eql?1.0
 => false

Code speaks itself! eql? is handy when we want to differentiate float and integer.

Hash

2.2.0 :008 > h1={a:1, b:2}
 => {:a=>1, :b=>2}
2.2.0 :009 > h2={a:1.0, b:2.0}
 => {:a=>1.0, :b=>2.0}
2.2.0 :010 > h1.eql?h2
 => false
2.2.0 :011 > h1==h2
 => true

eql? should be preferred in comparing hashes.

Strings

2.2.0 :006 > "foo"=="foo"
 => true
2.2.0 :007 > "foo".eql?"foo"
 => true

It doesn't matter which one we use if we are comparing strings.

Reminder to self: Someday I will comeback and write in detail what is happening in each case.

Loading

Interested In Working Together ?

Over 8 years of industry experience in Game/Frontend
development with special interest.
Book your Consultation
© Prakash Poudel