equivalence in Ruby

number ==

x = 1

x == 1      # true
x == 1.0    # true
x == "1"    # false

number eql?

x = 1

x.eql?(1)   # true
x.eql?(1.0)   # false
x.eql?("1")   # false

string

x = 'Hello'
x == 'Hello' # true
x.eql? 'Hello' # true. value equality.
x.eqlal? 'Hello' # false. identity equality.
x.object_id   #
'Hello'.object_id  #
# when case
===

# value
==

# regexp
=~

# This is used by Hash to test members for equality.
# For objects of class Object, eql? is synonymous with ==.
eql?

# have same object_id
equal?

Resources

相关文章: