Equality of Data Structures: Ruby vs. JavaScript

Anna Rankin - Apr 7 '19 - - Dev Community

Skip to the TL;DR

In recent weeks I've been reading through JavaScript Allongé by Reginald Braithwaite (an excellent look at the fundamentals of JS through a functional programming lens) with a coworker. My coworker brought up something that didn't make sense to her:

In JavaScript, why isn't an empty array equal to an empty array? What's the difference between the two identical objects?

To someone who's been working with JavaScript for a while, the answer might seem straightforward: "They're two different arrays; of course one is not equal to the other!" A Rubyist like my friend, however, may see it differently: "These two arrays have the same content—what do you mean they're not equal?" As I see it, this is a philosophical difference between the meanings of the comparison operators in Ruby and JavaScript. One language uses the concept of object equivalence to compare data structures, while the other checks explicitly for object identity.

Note: I'm not going to discuss the JS double equal or Ruby's threequal operators, as these are more than just simple comparison operators (I also avoid using them because they can be confusing and misleading!).

Pixel art version of the Ruby programming language's logo

Equality of Data Structures in Ruby

When you compare data structures using the built-in behavior of == in Ruby, you're really comparing the contents of the object—and in the case of an array, you're also checking that the order of the elements is the same in both. This means that two variables that point to different objects in memory could be equal.

> first_array, second_array = [1,2,3], [1,2,3]

> first_array == second_array
=> true
Enter fullscreen mode Exit fullscreen mode

If for some reason you truly want to check and see that two variables refer to the same object, you can check object's ID—or preferably, use the .equal? method:

> first_array, second_array = [1,2,3], [1,2,3]

> first_array.object_id == second_array.object_id # 70176467875700 == 70176467875680
=> false
> first_array.equal?(second_array) # a clearer way to perform this comparison
=> false
Enter fullscreen mode Exit fullscreen mode

Unlike in JavaScript, the comparison operator in Ruby is actually a method defined on the class you're comparing (awesome walkthrough of this concept here). Since == is just a method, you can even override it if you like! This is a reasonable practice if you're writing custom classes that need to be compared to each other. Silly example below:

> class Ditto
*   def self.==(other)
*     true
*   end
* end

> Ditto == 'Pikachu'
=> true
> Ditto == 2
=> true
> Ditto == false
=> true
Enter fullscreen mode Exit fullscreen mode

Pixel art version of the JavaScript Logo

Equality of Data Structures in JavaScript

Unlike Ruby, JavaScript does not expose a unique ID for objects because it doesn't need to. Data structures are compared by identity by default. If two variables are equal, you can be sure that they point to the same object in memory.

> const firstArray = [1,2,3]
> const secondArray = [1,2,3]
> firstArray === secondArray
false
Enter fullscreen mode Exit fullscreen mode

If you want to check and see if two separate data structures have the same content, you'll have to write your own logic to check—or use a function from a library like Lodash.

// Super naïve implementation:
const arraysAreEqual = (array1, array2) => {
  return array1.every((el, index) => el === array2[index])
}
> arraysAreEqual([1,2,3],[1,2,3])
true
> arraysAreEqual([1,2,3],['a','b','c'])
false
Enter fullscreen mode Exit fullscreen mode

TL;DR

JavaScript's === checks to see if the two variables it's comparing point to the same data structure, while Ruby's == method checks to see if the contents of two arrays or hashes are equivalent.

RB vs. JS Equality - first image of two closed boxes & JavaScript logo, text

Ruby's [1,2,3] == [1,2,3] translates to [1,2,3].every((el, index) => el === [1,2,3][index]) in JS.

JavaScript's [1,2,3] === [1,2,3] translates to [1,2,3].equal?([1,2,3]) in Ruby.

References

Hopefully this helps you wrap your mind around what the two different languages expect when comparing data structures! If you're interested in going deeper, I've put together some references:

. . . . . . . . . . . . . .
Terabox Video Player