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
2
  • 2
    Because they're two different languages? Commented Jun 25, 2015 at 2:20
  • This is one area where JavaScript and Java are actually similar. Commented Jun 25, 2015 at 3:00

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.