Are Hashes in Ruby the same as Objects in JavaScript?

Anum Siddiqui

Often we compare hashes in Ruby to Objects in JavaScript. Although they share some similarities, the functionality of both are not the same. The similarities stem from the fact that Ruby’s hash and JavaScript’s object appear to look the same. Lets take a look at how each are created with their seemingly similar syntax.

Ruby hash:

  • sam = { name: “Sam”, age: 25, shoe_size: 9 }

JS object:

  • var sam = { name: “Sam”, age: 25, shoe_size: 9 }

Both are created in exactly a similar manner, with the usage of curly brackets -{}, and assigned their respective key, value pairs.

Another similarity both Ruby hashes and JavaScript objects share is that their values can be of any data type such as numbers, arrays, objects, hashes, strings etc. For example, the value of “:favorite_foods” key in our Ruby hash is equal to an array, likewise for the “favorite_foods ” property of our JavaScript object.

Ruby hash:

  • sam [:favorite_foods] = [“pizza”, “spaghetti”, “cheeseburger”]

=> [“pizza”, “spaghetti”, “cheeseburger”]

JS object:

  • sam.favorite_foods = [“pizza”, “spaghetti”, “cheeseburger”]

=> [“pizza”, “spaghetti”, “cheeseburger”]

*******************************************************************

The differences start to appear when it comes to accessing values from either of them. To access values, we obtain it from our Ruby hash from what is known as a key, however in Javascript it is known as a property. Ruby allows the keys to be versatile, meaning they can be of any data type such as string, symbol, number etc. On the other hand, Javascript doesn’t give us this option whereby it limits us to the property of the objects not being of any data type but in fact just being properties of the object.

Ruby hash:

sam[:age] => 25

JS object:

sam.age => 25

Another key difference to note about Ruby hashes and JavaScript objects is when it comes to functionality. When it comes to Ruby hashes we are restricted by means of which we are not able to assign a method as a hash value. This is solely due to the fact that methods in Ruby aren’t objects. On the other hand, JavaScript has a slight advantage over Ruby in the sense that in JavaScript object, a function can be passed as a value. In the example below, in order for sam to thank us in Javascript, we added the property “thank_you” and set its value equal to a function.

JS object:

sam.thank_you = function(name) { console.log(“Thank you, ” + name + “!”) };

=> function (name) function(name) { console.log(“Thank you, ” + name + “!”) }

sam.thank_you(“Jim”)

=> Thank you, Jim!

Conclusion:

In conclusion, hashes in Ruby and objects in Javascript in comparison may appear to look quite similar. However the functionality of both are quite different. While Ruby hashes serve to be versatile in their keys, allowing them to be of any data type, Javascript objects demonstrate versatility in its functionality allowing us to pass functions as values of its property!

References:

https://webdevjeffus.github.io/blog/javascript.html

https://www.slideshare.net/brianhousand/coding-superpower

Anum Siddiqui

Written by

New York native Full Stack Web Developer on the hunt for new opportunities and always hungry to learn!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade