1

Why is there such a difference when comparing 2 objects in Javascript and in Ruby ? (it is confusing)

in Javascript :

obj1 = [1,2];
obj2 = [1,2];

obj1 == obj2 
// false

in Ruby :

obj1 = [1,2];
obj2 = [1,2];

obj1 == obj2 
# true
harvey
  • 299
  • 1
  • 8

1 Answers1

4

In Javascript, arrays are objects, == will test if the two objects are the same instance. If you want to compare their contents, read Comparing two arrays in Javascript.


In Ruby, there are multiple ways to compare objects. == is used to compare values, so it makes sense that they are equal.

To compare if they are the same object, use equal?:

obj1.equal? obj2
# => false
Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294