Last active
December 12, 2016 20:23
-
-
Save 2-am-zzz/4e0313b56da3941d60b2512899a83226 to your computer and use it in GitHub Desktop.
Comparing JavaScript and Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Functions | |
function hello() { | |
console.log("Hello World") | |
} | |
// Calling a function | |
hello // function | |
hello() // "Hello World" | |
// Returning from functions | |
var helloTwo = function() { | |
return "Hello World Again" | |
} | |
console.log(helloTwo()) | |
// Functions with different args passed in | |
var hello_three = function(please, fill, out) { | |
console.log("We ran!") | |
} | |
hello_three("yass") // "We ran!" | |
hello_three("yass","queen","hello","fourth") // "We ran" | |
// Destructive Functions | |
var a = [1, 2, 3] | |
a.reverse() | |
a // [3, 2, 1] | |
// Each | |
var a = [1, 2, 3] | |
var b = [] | |
a.forEach(function(number) { | |
b.push(number + 1) | |
}) | |
// For | |
for (var i = 0; i <= 10; i++) { | |
console.log("Hello, string interpolation " + i) | |
} | |
// ES6 Way | |
for (var j = 0; j <= 10; j++) { | |
console.log(`Hello, string interpolation ${j}`) | |
} | |
// Objects | |
var h = {} | |
h.first = 1 | |
h.second = 2 | |
h.first // 1 | |
h['second'] // 2 | |
var place = 'second' | |
h[place] // 2 | |
h.place // undefined | |
h['third'] = function () { | |
console.log(3) | |
return 3 | |
} | |
h.fourth // undefined | |
h.third // function | |
h['third'] // function | |
h.third() // 3 | |
// Classes | |
function Dog(firstName, age, breed, secret) { | |
var secret = secret | |
this.firstName = firstName | |
this.age = age | |
this.breed = breed | |
} | |
Dog.prototype.woof = function() { | |
return this.firstName + "woofs." | |
} | |
var karl = new Dog("Karl",0,"Finnish Spitz", "Shhh") | |
var paul = new Dog("Paul",5,"Golden Retriever") | |
karl.woof() | |
karl.secret // undefined | |
karl.secret // Error | |
karl.firstName // "Karl" | |
karl.firstName = "Carl" | |
karl.firstName // "Carl" | |
// Conditionals | |
var arr = [] | |
if (arr) { | |
console.log("We did it!") | |
} | |
// Equality | |
undefined == null // true | |
undefined === false // false | |
[] == false // true | |
[] === false // false | |
0 == false // true | |
0 === false // false | |
0 == "" // true | |
"" == false // true | |
"" === false // false | |
null == false // false | |
[1, 2] == [1, 2] // false | |
[1, 2] === [1, 2] // false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Functions | |
def hello | |
puts "Hello World" | |
end | |
# Calling a function | |
hello # "Hello World" | |
# Returning from functions | |
def hello_two | |
"Hello World Again" # Will get returned | |
end | |
p hello_two | |
# Functions with different args passed in | |
def hello_three(please, fill, in) | |
p "It ran!" | |
end | |
hello_three("yass") # Error | |
# Destructive Functions | |
a = [1, 2, 3] | |
a.reverse! | |
a # [3, 2, 1] | |
a.reverse # [1, 2, 3] | |
a # [3, 2, 1] | |
# Each | |
a = [1, 2, 3] | |
b = [] | |
a.each do |number| | |
b << number + 1 | |
end | |
b # [2, 3, 4] | |
# For | |
for i in 0..10 | |
puts "Hello, string interpolation #{i}" | |
end | |
(0..10).each { |n| | |
puts "Hello, string interpolation #{n}" | |
} | |
# Hashes | |
h = {} | |
h['first'] = 1 | |
h['second'] = 2 | |
h['third'] # nil | |
h.third # Error | |
# Classes | |
class Dog | |
attr_accessor :name, :age, :breed | |
def initialize(name, age, breed) | |
@name = name | |
@age = age | |
@breed = breed | |
end | |
def woof | |
"#{@name} woofs." | |
end | |
end | |
karl = Dog.new("Karl",0,"Finnish Spitz") | |
karl.woof # Karl woofs. | |
# Equality | |
[] == false # false | |
nil == false # false | |
[1, 2] == [1, 2] # true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment