Ruby is "equivalent"

Is there a Ruby equivalent for Python "is"? It checks if two objects are identical (i.e., have the same memory location).

+6
python ruby
source share
2 answers

Use a.equal? b a.equal? b

http://www.ruby-doc.org/core/classes/Object.html

Unlike ==, equal? the method should never be overridden by subclasses: it is used to determine the identity of the object (i.e. a.equal? โ€‹โ€‹(b) iff a is the same object as b).

+12
source share

You can also use __id__ . This gives you an internal object identification number that is always unique. To check if the objects are the same, try

a.__id__ = b.__id__

So the standard Ruby library does this as far as I can tell (see group_by and others).

+2
source share

All Articles