Ruby vs JavaScript
So going back and forth between computer languages can have a bad effect on remembering which language uses that syntax or that language uses this syntax
You know you need help when your Ruby program is asking why you have a bunch of semicolons and your JavaScript program is asking what the heck def fizzbuzz num
is supposed to do.
For this reason, my cheatsheet will be comparing the two languages and referencing how to do what. For simplicity's sake, I'm only doing one way to define variables, objects, functions and so on in my preferential way.
Thing to Compare |
Ruby |
JavaScript |
Link to Docs |
Ruby 2.2.1 Doc |
JavaScript Mozilla Docs |
Printing to the Console |
print / puts / put / p |
document.write(); / console.log();
|
Variables |
my_variable = "..." |
var myVariable = "..."
|
Branching |
if var == 1/elsif var == 2/else/end |
if (var === 1)/else if (var === 2)/else
|
Loops |
counter = 0while counter < 5 counter += 1endarray.each do |thing| puts thingend |
counter = 0while (counter < 5){ counter++;};for (var i = 0; i < 5; i++){ console.log(i);} |
Hashes/Objects |
my_hash = {"dog" => 10, "cat" => 3, "fish" => 12} |
var anObject = {dog: "Jerry", cat: "Scotty", fish: "Boyd"} |
Functions |
def my_functionend |
var myFunction = function(){}; |