Ruby vs JavaScript


In this blog post I discuss the main differences between two programming languages, Ruby and JavaScript. After spending a year-and-a-half building Ruby on Rails applications, I concluded that the Ruby language is easier to learn than JavaScript; however, JavaScript's syntax is richer than that of Ruby.

Strings

When it comes to defining a string, both languages share nearly identical syntax save for the keyword "var" which JavaScript uses to define variables.

# Ruby
str = "Hello world"
# retrieve an element
str.index("H") #=> 0 
str.rindex("l") #=> 9 
# insert an element
a = "hello "
a << "world"   #=> "hello world"

// JavaScript
var str = "Hello world";
// To retrieve an element
str.indexOf("H"); //=> 0 
str.lastIndexOf("l"); //=> 9 
// To insert an element
var a = "hello ";
a.concat("world!"); //=> "hello world!"

Given an input with the string "Hello", let us assume that we want the string to output "HelloHelloHelloHello". The code below demonstrates how to accomplish this in both languages:

// required output "HelloHelloHelloHello" 
// JavaScript
new Array("Hello", "Hello", "Hello", "Hello").join(""); //=> "HelloHelloHelloHello"

# In Ruby we will simply type:
"Hello" * 4 #=> "HelloHelloHelloHello" 
"Hello" * 0 #=> ""
Classes

To create a class in Ruby the keyword “Class” is used followed by the class name with the first letter capitalized. Javascript uses the keyword “function” instead.

In the example below you may notice variables proceding the “@“ symbol; these are instance variables. There is more than one type of variable in Ruby. Also, in the Javascript code below I have used the keyword "this." Think of “this” as equivalent to Ruby “@."

# Ruby
Class Person
  def initialize (first, last) 
    @first = first 
    @last = last 
  end 
  def full_name
    @first + " " + @last 
  end 
end 
harry = Person.new("Harry", "Potter")
harry.full_name #=> "Harry Potter" 

// JavaScript 
function Person (first, last) {
  this.first = first;
  this.last = last; 
}; 
Person.prototype.fullName = function() { 
  return this.first + " " + this.last; 
}; 
var harry = Person.new("Harry", "Potter") harry.fullName() //=> "Harry Potter" 
Functions

Functions in Ruby are called methods. To define a method we need to declare the keywords "def" and "end" as demonstrated in the example below. In JavaScript we define a function by declaring the keyword "function." The function block is defined by a pair of curly braces "{ }."

# Ruby
def sum (a, b)
  a+b 
end 
sum # method invocation

// JavaScript
var sum = function (a, b) { 
  return (a + b); 
}; 
sum(); // function invocation

Because arrays and hashes are nearly identical in both languages, I have merely demonstrated their usage in both Ruby and JavaScript below.

Arrays
# Ruby 
# Define an array 
array = [1, "a", true] or Array.new(1, "a", true)
# remove an element 
array.delete_at(1) #=> "a" 
# retrieve the last element 
array.last #=> " true" 
# retrieve the first element 
array.first #=> 1 

// JavaScript
// Define an array 
var array = [1, "a", true]; 
// remove an element 
array.splice(1, 1); //=> "a" 
// retrieve the last element
array[array.length - 1]; //=> " true"
// retrieve the first element
array[0]; //=> 1
Hashes
# Ruby
# define a hash 
hash = { }
hash[‘a’] = 1 
hash[‘b’] = 2 
# Iteration 
hash.each { |key, value| puts "#{key} #{value}" } 
# retrieve all keys
hash.keys #=> ['a', 'b'] 

// JavaScript
// define a hash 
var hash = { }; hash[‘a’] = 1; hash[‘b’] = 2; 
// Iteration 
for (key in hash) { console.log(key, hash[key]); } 
// retrieve all keys
Object.keys(hash); //=> ['a', 'b']