Theory of Coding

Web Development blog

Tale of Two Languages

Ruby vs. Javascript

Tale of Two Languages

Ruby and Javascript are very difference languages in many ways such as their syntax, methods, and ways of defining functions. Ruby tends to be shorter and more compact but javascript seems to be more explicit in defining their values such as putting function(), and var in front of the object. Both are object oriented programming languages, where they have a class and objects and methods that associates with them are organized in a proper way.

Functions

Javascript has two ways of defining functions

  • function expressions
  • function declarations

Here is a function declaration

1
2
3
  function cat() {
    prompt("Do you like cats?")
};

Here is a function expression

1
2
3
  var cat = function(){
    prompt("Do you like cats?)
  };

Ruby on the other hand, has three types of functions

  • blocks
  • procs
  • lambdas

Here is a block

1
[1,2,3].each { |x| puts x*2 }

Here is a proc

1
2
p = Proc.new { |x| puts x*2 }
[1,2,3].each(&p)

Here is a lambda

1
2
lam = lambda { |x| puts x*2 }
[1,2,3].each(&lam)

Procs and lambdas seem to be closer to a function expression in javascript, because you can set a block to a variable and then call it later.

Iterations

In Ruby:

1
2
3
4
arr = [1, 2, 3, 4]
for item in arr
    puts item
end

In Javascript:

1
2
3
4
var arr = [1, 2, 3, 4];
  for (var i = 0, len = arr.length; i < len; i++) {
    console.log(arr[i]);
}

In this way, javascript seems to be more explicit in showing the types of each element of the method. Javascript specifies var for the variable, and also it lists out the variable to start iteration, the condition for the loop to run, and what happens after each iteration.

Since ruby and javascript are quite different they also have similar methods, that are sometimes called different things but mean the same thing.

Ruby Javascript
push push
reverse reverse
map map
index indexOf
all? every
any? some
slice slice
each forEach
index indexOf
split split
has_key? hasOwnProperty
to_i parseInt
.class typeof()

Methods that do not exist in Javascript:

  • include?

Other Comparisons:

Ruby Javascript
puts console.log()
nil, false 0, null, false, “”, undefined, NaN

Return is usually used in Javascript, but not in Ruby, because Ruby returns the value of the last line in the method. Javascript will not return it without it explicitly being called return.

Arrays

1
2
3
4
5
 Ruby              vs.       Javascript
| a = ["1", "2"]             | var a = ["1", "2"];|
|a.push("3")                 | a.push("3"); |
|a.all?{|n| n > 4}           | a.every(function(n) { return n > 4; });
|a.each {|n| puts n}         |a.forEach(function(n) { console.log(n); })

Hashes

1
2
3
4
5
6
7
8
9
10
11
12
 Ruby:
hashPetName = { "dog" => "Rex", "cat" => "Mistigri" }

hashPetName['dog'] = "Rex"

Javascript:
var hashPetName = {
    dog: "Rex",
    cat: "Mistigri"
  }

hashPetName['dog'] = "Rex";

Functions

1
2
3
4
5
6
7
8
9
Ruby:
def sample_func
  puts "Hello World"
end

Javascript:
function sample_func() {
  console.log("Hello World");
};

Classes

Javascript’s equivalent to Ruby’s class is called a Prototype. To make a new Prototype, just create a new function with a name with aguments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Ruby:
class Person
  attr_accessor :firstName, :lastName

  def initialize(firstName, lastName)
    @firstName = firstName
    @lastName = lastName
  end

Javascript:
function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

Math

1
2
3
4
5
Ruby:
[-5, 15, 20].reduce(0, &:+) # 30

Javascript:
[-5, 15, 20].reduce(function(sum, value) { return sum + value; }, 0) // 30