What has been in the works lately? It'll blow your mind.

Generic placeholder image

This stuff is pretty amazing

JavaScript

JavaScript and Ruby are both considered Object Oriented Programming languages. But, the ways they define objects syntactically are different.

Ruby Hashes vs. JavaScript Objects

Ruby Hashes resemble the creation of a JavaScript object. In Ruby a hash is a a list of key value pairs. The syntax is for a Hash in ruby is written below:

 my_hash = { key1: value1, key2: value2}

Creation of a object in JavaScript has very similar syntax. Object definition in JavaScript is a variable defined with keyword var and the variable name. It is followed by curly brackets that define a pairs of property names and values which can be any number of objects. The syntax to define a JavaScript object is written below:

var myJavaScriptObject = {
      property1: value1,
      property2: value2

      behavior1: function() {
        this.property1 +=1;
      }
    };

The javaScript object is very much like a Hash defined in ruby. Even the name of a function called behavior1 is a property name. The property behavior1 is a function which will increment the property1 by 1.

Ruby classes vs. JavaScript constructor functions

Ruby classes are defined in with a class keyword and it is characterized by attributes and methods and a very special method called initialize. Even though JavaScript doesn't seem to have a formal class definitions for its object. It can achieve the creation of objects easily with a constructor method for its objects similar to ruby.

Ruby classes have several important characteristics which are its properties, behavior and within its behavior a special method when instantiation occurs. This method is the initialize method. They syntax for a ruby class is written below:

class My_object

      @instance_variable1
      @instance_variable2

      def initialize(value1, value2)
        @instance_variable1 = value1
        @instance_variable2 = value2
      end

      def method_behavior_1
        @instance_variable1 += 1
      end


    end

Within this ruby class called My_object we have the initialize method that takes 2 parameters to set the two instance variables. We also have 2 instance variables and 2 method which can change the state of the object by incrementing the @instance_varaible1 property.

Object instantiation occurs in ruby when the Object method new is used to instantiate an instance using the class. With the following syntax written below:

my_cool_object = My_object.new( 1, 2)

In the line above we are instantiating a new object called my_cool_object with the new method. new uses the 2 parameters to pass to the initialize method that is the special method we talked about earlier. During instantiation with new we actually call initialize at the time of the creation of the instance of My_object.

Even though JavaScript does not have a formal class definition as Ruby does, it can establish an object with the syntax described earlier with state and behavior and it can use constructor method that is called when the special keyword new is used to create an object. constructors in JavaScript are the equivalent of ruby's initialize methods.

A similar JavaScript object can be created that we just created in Ruby

var myJavaScriptObject = {
      property1: value1,
      property2: value2
    };

    function javaScriptObject(value1, value2){
      this.property1 = value1;
      this.property2 = value2;
    };

We can instantiate new object in JavaScript also with this constructor method that is given a name of a category of objects which could be thought of a class. The new objects are of the type javaScriptObject. We call the constructor by writing the line below:

 var myCoolJavaScriptObject = new javaScriptObject(1, 2);

  the above line will achieve a similar result to:

  var myCoolJavaScriptObject = {
    property1: 1,
    property2: 2
  };

Looping in Ruby vs. Looping in JavaScript

There are some important looping differences between Ruby and Java. Ruby uses .each to create iterator methods. Using each greatly reduces syntax. for loops are used to iterate over arrays in JavaScript.

Below is an example of a ruby .each syntax that iterates over an array

my_array = [1,2,3, 4]

my_array.each { |element_of_array| puts element_of_array}

Below is an example of a JavaScript iteration over an array using a for loop.

var my_array = [1, 2, 3, 4];

for(var index = 0; index < my_array.length ; index += 1)
      console.log(my_array[index]);