Ruby/Class/equal

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

== and equal" are different

a = "Ruby"    # One String object
b = "Ruby"    # A different String object with the same content
a.equal?(b)   # false: a and b do not refer to the same object
a == b        # true: but these two distinct objects have equal values



Same value but different reference

a = "Ruby"       # One reference to one String object
b = c = "Ruby"   # Two references to another String object
a.equal?(b)      # false: a and b are different objects
a.object_id == b.object_id   # Works like a.equal?(b)



Use equal? to check the references

a = "Ruby"       # One reference to one String object
b = c = "Ruby"   # Two references to another String object
a.equal?(b)      # false: a and b are different objects
b.equal?(c)      # true: b and c refer to the same object