Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created February 21, 2022 17:43
Show Gist options
  • Save caioertai/7b9e5eb004b0a844fcc6e0a32f568bc6 to your computer and use it in GitHub Desktop.
Save caioertai/7b9e5eb004b0a844fcc6e0a32f568bc6 to your computer and use it in GitHub Desktop.
// JS
// Print on the console
console.log("Hello world!")
# Ruby
# Print on the console
puts "Hello world!"
// Assign a value to a variable
// (binding)
// That might be reassigned later on
let message = "Hello"
console.log(message)
// Reassign a value
message = "Olá"
console.log(message)
console.log("---=---")
// That will NOT be reassigned
const message2 = "Hello"
console.log(message2)
// CANNOT (WILL CRASH) BE REASSIGNED
// message2 = "Olá"
// console.log(message2)
// ---------------------
let age = 20
console.log(age)
// Increment
// age = age + 1
// age++
age += 1
console.log(age)
// Old style (DON'T USE)
// var message = "Hello"
# Assign a value to a variables
message = "Hello"
p message
# Reassign a value
message = "Olá"
p message
age = 20
p age
# Increment
# age = age + 1
age += 1
p age
const greeting = "Hello World";
// Return the length of a string
console.log(
greeting.length
)
// Get some or all elements of a string
console.log(
greeting[4]
)
// All after 2
console.log(
greeting.substring(2)
)
// From 2 to 6
console.log(
greeting.substring(2, 6)
)
// Upcase, downcase a string
greeting.length // an attribute of string
console.log(
// Is function
greeting.toUpperCase()
)
console.log(
greeting.toLowerCase()
)
// No capitalize is JS
// Split a string
console.log(
greeting.split(" ")
)
// Concatenate 2 strings
console.log(
"Something" + "!"
)
// Be careful with the auto casting
console.log(
1 + "2"
)
// Interpolate strings
const word1 = "Hello"
const word2 = "Again"
console.log(
`${word1} ${word2}!`
)
greeting = "Hello World"
# Return the length of a string
p greeting.length
p greeting.size
# Get some or all elements of a string
p greeting[1]
p greeting[2..5]
# Upcase, downcase a string
p greeting.downcase
p greeting.upcase
p greeting.capitalize
# Split a string
p greeting.split # default is " "
greeting.split(" ")
# Concatenate 2 strings
p "Something" + "!"
# Be careful with the auto casting
# p 1 + "2" # DOES NOT WORK
# Interpolate strings
word1 = "Hello"
word2 = "Again"
p "#{word1} #{word2}!"
// const or let?
// const fruits = ["Apple", "Orange", "Tomato", "Cupuaçu", "Bacuri"]
// console.log(fruits)
// // CRUD
// // Create
// fruits.push("Pineapple")
// console.log(fruits)
// // Read
// console.log(fruits[1])
// // Update
// fruits[1] = "Banana"
// console.log(fruits[1])
// // Delete
// console.log(fruits)
// // index, #
// fruits.splice(1, 1)
// console.log(fruits)
// Iterate over an array
const fruits = ["Apple", "Orange", "Tomato", "Cupuaçu", "Bacuri"]
fruits.forEach((fruit) => {
console.log(fruit.toUpperCase())
})
// Anonymous Function
// () => {}
// forEach receives a function as argument
# fruits = ["Apple", "Orange"]
# p fruits
# # CRUD
# # Create
# # fruits.push("Pineapple")
# fruits << "Pineapple"
# p fruits
# # Read
# p fruits[1]
# # Update
# fruits[1] = "Banana"
# p fruits[1]
# # Delete
# fruits.delete_at(1)
# p fruits
# Iterate over an array
fruits = ["Apple", "Orange", "Tomato", "Cupuaçu", "Bacuri"]
fruits.each do |fruit|
puts fruit.upcase
end
# Block (anonymous function)
# do |fruit|
# end
# { |fruit| }
# Array#each receives a block as argument
// Can you vote? in JS
const age = 18;
if(age >= 18) {
console.log("You can vote!")
} else {
console.log("You cannot vote!")
}
// Elsif / else if in JS
if(age > 18) {
console.log("You can vote!")
} else if(age === 18) {
console.log("You can barely vote!")
} else {
console.log("You cannot vote!")
}
// What is falsy in JS?
undefined
null
false
0
''
NaN
// Ternary operator
const result = age >= 18 ? "can vote" : "cannot vote"
console.log(result)
# Can you vote?
age = 18
if age >= 18 # truthy / nil false
puts "You can vote"
else
puts "You cannot vote"
end
# Elsif / else if
if age > 18
puts "You can vote"
elsif age == 18
puts "You can barely vote"
else
puts "You cannot vote"
end
# What is falsy?
nil
false
# Ternary operator
result = age >= 18 ? "can vote" : "cannot vote"
p result
student = {
name: "Ringo",
age: 23
}
p student
# CRUD
# Create
student[:grade] = 10
p student
# Read
p student[:age]
# Update
student[:grade] = 9
p student
# Delete
# student.delete(:age)
# p student
# Iterate over it
# keys = student.keys
# keys.each do |key|
# puts "#{key} --- #{student[key]}"
# end
student.each do |key, value|
puts "#{key} --- #{value}"
end
const student = {
name: "Ringo",
age: 23
}
console.log(student)
// CRUD
// Create
// student["grade"] = 10
student.grade = 10
console.log(student)
// Read
// console.log(student["grade"])
console.log(student.grade)
// Update
// student["grade"] = 9
student.grade = 9
console.log(student)
// Delete
delete student["age"]
console.log(student)
// Iterate over it
const keys = Object.keys(student)
keys.forEach((key) => {
console.log(key, student[key])
})
// In JS...
// Arrow function () => {} is an anonymous function...
// ... unless you give it a name...
// const myFunction = () => {}
// A capitalize function
const capitalize = (string) => {
const firstLetter = string[0].toUpperCase();
const remainder = string.substring(1).toLowerCase();
debugger
return `${firstLetter}${remainder}`; // firstLetter + remainder
}
console.log(
capitalize("jOhn")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment