Ruby Vs. Javascript
The Final Battle
December 7, 2015
Going from Ruby to Javascript felt a little bit like trying to pick up French as a casual Spanish speaker. There are some overlaps, to be sure. Enough to make sense of basic things. But, each is its own language with its own grammatical rules. Fortunately, I like both of them!
Javascript Objects are similar to Ruby Hashes in that they are data sets organized by key/value (or property/value) pairs. In Javascript, an object must be declared as a variable, like so:
I've declared 'myObject' as a variable, and filled it with two property/value pairs. These properties are separated by commas, and a trailing semicolon completes the object. In Ruby, I could create a hash like so:
The Ruby syntax is just a bit simpler, since I don't need to declare the hash as a variable, nor end with the semicolon. In Ruby, I can use either symbols (key:) or strings ('string') -- or really ANY object -- as a key value.
Accessing values is slightly different in Ruby and Javascript as well. In Javascript, I use the .propertyName call on the object name, whereas in Ruby I use the [key] to access the value.
There is another way to access the values in both languages, using square bracket notation and strings.
To create a new value in a Javascript Object, you simple use the .propertyMethod name on the Object and the property and value will be added to the end of the list inside the Object. In Ruby, you can add to a hash using the square bracket hash[key] = value notation.
Ruby and Javascript blissfull overlap, but are still each their own animal. I look forward to learning more about these languages.