Javascript And Ruby

Screen Shot 2017-08-25 at 4.22.05 PM

Ruby On Rails was the first programming language I was exposed to, and looking back, I am glad it was. I think it helped that it heavily uses English in a way that a newbie can understand, so it made learning to code less intimidating. However, it also means when it was time to learn my second language, Javascript, I recall staring dumbfounded at the screen thinking one thing only: “WHAT THE F*** KIND OF AN ALIEN LANGUAGE IS THIS!?!?!”

Thankfully, my relationship with Javascript improved when I tinkered about with my first Javascript challenge: reversing a string. Before working on the program, I sat down and compared Ruby syntax with Javascript to make a note of the similarities and differences. That was a life-saver! Here is the first batch of just some observations I made back then that really helped me break Javascript into something I could more easily grasp:

1. Variables: 

Javascript’s variables are just slightly longer than Ruby. In addition to the variable and the value, you also have to include”var” and a semi-colon (in fact, make sure you end every line in your program with a semi-colon) like so:

Ruby: 

best_pet = "cats"

Javascript:

var best_pet = "cats";

2. Debugging/Printing Variables and Interpolation:

Ruby: In Ruby, it’s all about good old”puts”. Also, to insert the variable in a string, we use interpolation:

puts "my cat is the #{best_pet}"

Javascript: In Javascript, we use console.log. After you declare a variable in Javascript, do not refer to that same variable with a var. Just refer to the name. Also, just an fyi: please be very careful not to include the variable in the preceding string! It’s a very easy typo to make, and one that has caused me tremendous existential angst in later programs:

console.log("my cat is the" + best_pet);

3. If/Else Statements:

Ruby: We’ve got the usual if/else/elsif conditional flow:

learning_to_code = true
human_being = true

if learning_to_code && human_being
   puts "You are awesome!"
elsif learning_to_code && !human_being 
   puts "Wha.....!!?"
end 

Javascript: Similar, but with an “else if” and some of the other syntactic differences pointed out already:

var learning_to_code = true;
var human_being = true;

if (learning_to_code && human_being) {console.log("You are awesome!");
}
else if (learning_to_code && !human_being) {console.log("Wha....!!?");
}

4. Functions/Methods:

Ruby: Javascript functions are Ruby’s methods:

def cat(name) 
 puts "Hello" + name.to_s
end

Javascript: And, of course, Ruby’s methods are Javascript’s functions:

function cat(name) {
return " Hello " + name
}

5. For Loops: 

I think loops look much less complicated in Ruby than in Javascript. Here’s what I mean:

Ruby:

"Meow".chars.each do |letter|
   puts letter
 end

Javascript:

var string = "Meow!";
for (var i = 0; i < string.length; i++) { 
   console.log(string[i]);
}

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s