Ruby Hashes Versus JavaScript Objects
December 11, 2014
In Ruby, an object can be any data structure or value, from strings and ints, to arrays and hashes. Objects also understand certain methods, which are executable routines the object can trigger. Objects are instances of a class, which define groups of behavior and functionality. Classes can be written and modified to create objects with certain attributes and behaviors. In JavaScript, an object is a variable that contains many properties with many values. Let's break that down. The following code creates a variable named captain
.
var captain = "Katherine Janeway"
This variable points to a single value, in this case the string "Janeway". If we wanted to assign many values to the variable, it becomes an object. Each value is paired with a particular property. The syntax is as follows:
var captain = { name: "Katherine Janeway", ship: "USS Voyager", favorite drink: "coffee, black" }
As such, Javascript objects are variables too, but can contain many values. Above, captain
has many values ("Katherine Janeway", "USS Voyager", "coffee, black"). The values are paired with the particular property they describe (name, ship, favorite drink). Each property is written as a name, followed by a colon, followed by the value. Each property/value pair is separated by a comma. All property/value pairs are enclosed in curly braces.
To access the properties and the values inside JavaScript objects, one can use the dot notation or square brackets: captain.name
or captain[name]
. The difference between the two is how name
is interpreted. With dot notation, the part after the dot must be a property named in the object. With square brackets, the expression in the brackets is evaluated to get a property name. To create a new value for a property, use = after the call. The property's value will be replaced or a new property created if it does not exist.
Seeing a Javascript object and especially how values are accessed in those objects, it becomes apparent that Javascript objects are very similar to Ruby hashes. A hash in Ruby is a collection of objects (and is an object itself). It consists of key/value pairs. Any Ruby object can be a key or value. Let's create a hash consistent with the above captain
.
captain = { "name" => "Katherine Janeway", "ship" => "USS Voyager", "favorite drink" => "coffee, black"}
Above, the captain
hash contains keys ("name", "ship", "favorite drink") that point to their respective values ("Katherine Janeway", "USS Voyager", "coffee, black") using =>. Each key/value pair is separated with a comma, and all are enclosed in curly brackets. To access a value, square brackets are used: captain["favorite drink"]
will return "coffee, black"
. To add/modify a key/value pair, similar to JavaScript objects, use the = operator: captain["OTP"] = "Seven of Nine"
.
In conclusion, Ruby hashes and JavaScript objects are very similar conceptually. They both relay the same type of paired property/value information. They differ syntactically in their creation, but are strikingly similar with respect to accessing their values.