10 Differences Between Ruby and JavaScript

Aymes S.
Aymes S.
Oct 9, 2020 · 4 min read

1. Printing Out the Functions

In Ruby, we are familiar with “puts, print, p, and pp.” In Javascript, we use “console.log()” to have a return.

def who_lives_in_a_pineapple_under_the_sea
puts "Spongebob Squarepants!"
end
function whoLivesInAPineappleUnderTheSea(){
console.log("Spongebob Squarepants!");
}

2. The Each Method

This method is a way to iterate through an array of elements in order to return, change, or manipulate them.

spongebob_chars = ["Spongebob", "Patrick", "Sandy", "Plankton"]
spongebob_chars.each {|single_char| puts single_char}
#=> "Spongebob", "Patrick", "Sandy", "Plankton"
let spongebobChars = ["Spongebob", "Patrick", "Sandy", "Plankton"]
spongebobChars.forEach(function(singleChar) {
console.log(singleChar);
}
//=> "Spongebob", "Patrick", "Sandy", "Plankton"

3. Hashes and Objects

Ruby:

spongebob_hash = {name: "Spongebob Squarepants", pet: "Gary the Snail", occupation: "Fry Cook"}spongebob_hash[:pet] #=> "Gary the Snail"
let spongebob = {name: "Spongebob Squarepants", pet: "Gary the Snail", occupation: "Fry Cook"};spongebob.pet //=> "Gary the Snail"

4. Determining a Value of Something

Ruby:

squidward = "Squidward"squidward.class #=> String
let squidward = "Squidward";typeof(squidward) //=> String

5. Making Comments

Ruby:

# f is for friends who do stuff together
// u is for you and me
/* n is for anywhere
and anytime at all
down here in the deep
blue sea! */

6. Incrementing and Decrementing

To increment or decrement by 1 in JavaScript, we can write ++or - -. In Ruby, it is +=. We can also write += in JavaScript if we want to increment or decrement by a number larger than 1. (+= 2 to add by two or -=5 to subtract by 5)

while x < 5
puts x
x += 1
end
let x = 0; 
while (x < 5){
x++;
console.log(x);
}

7. == vs ===

Ruby only has = and == while JavaScript has all three, =, ==, and ===.

25 == "25"
//=> true
25 == 25
//=> true
25 === "25"
//=> false
25 === 25
//=> true

8. Declaring Variables

Both Ruby and JavaScript have different ways of defining variables.

x = "krabby patty" 
# can only be used in its scope
GLOBALVARIABLE = "krabby patty"
# can be used throughout the whole program
# note that the variable name must be capitalized
var plankton = "Plankton";function getkrabbyPatty {
var secretFormula = "The Secret Formula";
}
console.log(secretFormula);
//=> secretFormula is undefined
for {let x = 1; x < 5; x++
console.log(x);
}
//=> 1, 2, 3, 4
const krabbypatty = "Krabby Patty";
const krabbypatty = "Pretty Patty";
//=> error 'krabbypatty' has already been declared

9. If/Else and Switch Statements

Ruby and JavaScript both have if/else statements, but JavaScript also has something called Switch statements. Remember that we useIf if a specified condition is true, else if the same condition is false, and elsif (Ruby) or else if (JavaScript) if the first condition is false.

puts "Who's Dirty Dan?"
dirtydan = gets.chomp # getting a user's input
if dirtydan == "Spongebob"
puts "Spongebob is Dirty Dan"
elsif dirtydan == "Patrick"
puts "Patrick is Dirty Dan"
else
puts "Try again"
end
if (dirtydan === "Spongebob"){
console.log("Spongebob is Dirty Dan";
} else if (dirtydan === "Patrick"){
console.log("Patrick is Dirty Dan");
} else {
console.log("Sandy's going to find out either way!");
}
var dirtydan = "Patrick";
switch (dirtydan){
case "Spongebob":
console.log("Spongebob is Dirty Dan");
break;
case "Patrick":
console.log("Patrick is Dirty Dan");
break;
default:
console.log("Sandy's going to find out either way!");
}

10. String Interpolation

Ruby:

mayonnaise = "Mayonnaise"
puts "Is #{mayonnaise} an instrument?"
let mayonnaise = "Mayonnaise"; 
console.log(`Is ${mayonnaise} an instrument?`);

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Aymes S.

Written by

Aymes S.

Software Engineer in the making.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store