On JS and Ruby

This blog is about my musing on the differences between Javascript and Ruby, especially concerning values, variables and scope in both languages.

Values
Values are virtually the same in Ruby and Javascript. Both are weak/loosely typed and possess strings, integers, booleans and the like. The striking difference though happens when you try to do some bizarre operations gymnastics with values in each language.

Take Ruby for instance, one would never be able to add an integer to a string.


1 + "1"
#=> TypeError: String can't be coerced into Fixnum 


But in Javascript…


1 + "1"
// = "11"

Craaaazyyy, right.

There is more, check this out:


1 + 1 + "1"
// ="21"

"1" + 1 + 1
// = "111"

Even crazier. In Javascript, calculations occur from left to right until it finds a string; from then on the entire value will transformed into a concatenated string.

Another peculiarity of Javascript is what the increment operator does to values.

say we have a variable set to a string.


var x = "1";

if we increment using “++” we have:


x ++;
// = 2

So, “++” actually treats x as an integer and goes on to apply arithmetic to it.

However, if we had done:


var x = "1";

x += 1;
//= "11"

The “+=” increment treats x now as a string and concatenates every value after the increment to the string.

Variables

Each language has its own way of setting variables.

In Javascript, you set your variables by writing “var” in front of the variable name.

For example:


var name = "Gustavo";

In Ruby you only need the name variable, the equal sign and the value.


name = "Gustavo"

Scope

Global and local variables exist in both languages. The difference between Ruby and Javascript as far as scope is concerned is that in Javascript you can reset your global variable through a local variable.

Let me show you how:


var a = "sarah";
function dance_with(some_var){ 
  a = "gus";
  return(some_var + " dances with " + a );
}


function dance_with(a);
// = "gus dances with gus"

As you can see the variable “a” was reassigned from “sarah” to “gus”. This occurs because in Javascript it is possible to access global variables inside functions.
When you define a local variable named exactly like the global variable, Javascript thinks that you are redefining it during the function call.

This is an example of how Javascript access global variables via functions


var a = "sarah";
function dance(){ 
  return("dance " + a );
}

dance();
// = dance sarah

The global variable a could be accessed through the dance() function.

In Ruby that would not happen as you need to pass the global variable as a method argument.


a = "sarah"

def dance
 "dance " + a
end

dance
#=> NameError: undefined local variable or method `a' for main:Object

But


a = "sarah"

def dance(arg)
 "dance " + arg
end

dance(a)
#=> "dance sarah"

The lesson here is to be careful about how you name your local and global variable in Javascript. Make sure they are not the same, otherwise they mean trouble.

Lastly, I want thank two lovely ladies, Sarah and Uzo, for spending Sunday afternoon with me. Without them my rumination on the differences of Ruby and Javascript would have been utterly dull. Bouncing ideas off of them inspired me to write this Pulitzer-prize deserving blog post 🙂

Leave a comment