DEV Community

Thomas Kinsella
Thomas Kinsella

Posted on • Edited on

Ruby Hashes VS Javascript Objects

During my time learning at Flatiron School, we have focused primarily on Javascript as our front-end language and Ruby as our back-end language. Switching back and forth between languages has been difficult at times due to their similarities, as well as differences, in syntax. For me, one of the more tricky data types to differentiate in Ruby were hashes. Hashes are basically Ruby's version of Javascript Objects. Hashes and Objects look very similar in that they are both made up of key value pairs, enclosed in curly brackets {}. They share some similarities but also have their differences. In this blog post I hope to explain how they are similar, but also point out their key differences.

Ruby hashes and Javascript objects have very similar syntax and look very much alike. Let's create a Ruby hash and a Javascript object and see what I mean.

Ruby Hash

my_dog = { name: "oliver", age: 8, breed: "yellow lab" }
Enter fullscreen mode Exit fullscreen mode

Javascript Object

const myDog = { name: "oliver", age: 8, breed: "yellow lab" }
Enter fullscreen mode Exit fullscreen mode

As you can see, they look very similar but do have some differences. First, in Ruby we do not have to use let or const to save our data to a variable. Also, naming conventions are slightly different between the two. In Ruby we use snake case and in Javascript we use camel case. Snake case is separating each word with an underscore like so: my_variable. Camel case has no spaces and the name of our variable starts with a lowercase letter and each following word begins with an uppercase letter like so: myVariable.

Both our my_dog Ruby hash as well as our myDog Javascript object have keys and values. my_dog has a key of name with a value of "oliver". myDog has a key of age with a value of 8. One of the main differences between Ruby hashes and Javascript objects is that our keys on our Ruby hashes can be of any data type. For example we can have strings as keys, ex: "name": "oliver". In Javascript objects, our keys are just keys on an object. Our values though can be of any datatype in both Ruby hashes as well as Javascript objects.

Say we wanted to access the age of our myDog object in Javascript. We would use dot notation like so:

myDog.age

// 8 //
Enter fullscreen mode Exit fullscreen mode

Say we wanted to access the age of our my_dog hash in Ruby. We would have to use bracket notation like so:

my_dog[:age]

// 8 //
Enter fullscreen mode Exit fullscreen mode

This shows another main difference between Ruby hashes and Javascript objects. The way we access values from them is different. In our example above, we have to use different syntax to access the values from our my_dog Ruby hash and our myDog Javascript object. In Ruby, when we create a hash like we created my_dog, Ruby creates the hash with symbols as keys. Due to this, we can not use dot notation like we can in Javascript. We must use bracket notation like shown above, putting the symbol inside of the brackets, telling ruby that we want the value corresponding to this symbol key.

Lastly, a main difference between Ruby hashes and Javascript objects is that we cannot assign a Ruby method as a value in a hash. In Javascript, an object can have a function as a value but a Ruby hash can not have a method as a value. This allows Javascript objects to have some more functionality.

Overall, Ruby hashes and Javascript objects may look very similar and use a lot of the same syntax but when you take a deeper look you can see that there are some pretty major differences between the two.

Top comments (0)