Created
April 13, 2022 16:57
-
-
Save caioertai/6dbf44d4824fc170b5088f748805dc32 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# RECAP | |
# On representation: | |
# 1. The name of a dog. | |
# String | |
# Delimited by "" or '' | |
# 2. The age of a person. | |
# Integer | |
# 23 | |
# 3. The height of a person in meters. | |
# Float | |
# 1.65 | |
# 4. Affirmation of negation. | |
# boolean | |
# true / false | |
# 5. Process | |
# methods | |
# parameters | |
def greet_user(user_name, times) | |
times.times do | |
puts "Hello, #{user_name}!" | |
end | |
end | |
# arguments | |
greet_user("John", 5) |
This file contains hidden or 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
## Live-code: Interactive voting age program | |
age = gets.chomp.to_i | |
if age >= 18 | |
puts "You can vote!" | |
else | |
puts "You cannot vote!" | |
end | |
## Truthy | |
## Everything counts as true, except nil and false. | |
if true | |
puts "true -> Yes" | |
end | |
if false | |
puts "false -> No" | |
end | |
if nil | |
puts "nil -> No" | |
end | |
if '' | |
puts "'' -> Yes" | |
end | |
if 0 | |
puts "0 -> Yes" | |
end | |
if [] | |
puts "[] -> Yes" | |
end | |
## Using inline conditionals | |
puts "It prints" if true | |
## Unless (reverse of if) | |
age = gets.chomp.to_i | |
unless age >= 18 | |
puts "You cannot vote" | |
end |
This file contains hidden or 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
# Live-code: Let's flip coins | |
puts "Pick heads or tails" | |
user_guess = gets.chomp | |
result = ["heads", "tails"].sample # "heads" / "tails" | |
if result == user_guess | |
puts "You win!" | |
else | |
puts "You lost!" | |
end | |
# Can we make it shorter? Yes! | |
# Ternary operator | |
# .. . .. . .. | |
# condition ? true statement : false statement | |
puts result == user_guess ? "You win!" : "You lose!" |
This file contains hidden or 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
# Live-code: Morning? Afternoon? Evening? Lunch time! | |
hour = gets.chomp.to_i | |
# Say to which fraction of the day the hour belongs. | |
if hour < 12 | |
puts "Morning" | |
elsif hour > 18 | |
puts "Evening" | |
elsif hour > 12 | |
puts "Afternoon" | |
else | |
puts "Lunch time!" | |
end |
This file contains hidden or 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
# Live-code: Old School UI | |
puts "You can (type it below):" | |
puts "- read" | |
puts "- write" | |
puts "- exit" | |
user_input = gets.chomp | |
# A bit repetitive on the comparisons | |
if user_input == "read" | |
puts "TODO: reading" | |
elsif user_input == "write" | |
puts "TODO: writing" | |
elsif user_input == "exit" | |
puts "TODO: exiting" | |
else | |
puts "Not valid." | |
end | |
# case makes it cleaner | |
case user_input | |
when "read" | |
puts "TODO: reading" | |
when "write" | |
puts "TODO: writing" | |
when "exit" | |
puts "TODO: exiting" | |
else | |
puts "Not valid." | |
end | |
# You can inline cases with then | |
case user_input | |
when "read" then puts "TODO: reading" | |
when "write" then puts "TODO: writing" | |
when "exit" then puts "TODO: exiting" | |
else | |
puts "Not valid." | |
end |
This file contains hidden or 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
# Live-code: Opening hours | |
# Print the state of a shop that opens at 8, | |
# closes at 19, but is closed at 12 | |
puts "At what time?" | |
hour = gets.chomp.to_i | |
## A bit repetitive, but it works | |
if hour >= 8 && hour < 12 | |
puts "It's open!" | |
elsif hour > 12 && hour < 19 | |
puts "It's open!" | |
else | |
puts "It's closed!" | |
end | |
## With || (or) | |
if (hour >= 8 && hour < 12) || (hour > 12 && hour < 19) | |
puts "It's open!" | |
else | |
puts "It's closed!" | |
end |
This file contains hidden or 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
# Live-code: Find the right random number | |
number_to_find = rand(1..5) | |
# Give user the option to guess the number | |
# Print the result of the guess. | |
puts "Which number was it?" | |
user_guess = gets.chomp.to_i | |
# Option with while | |
while number_to_find != user_guess | |
puts "Try again!" | |
user_guess = gets.chomp.to_i | |
end | |
# Option with until (preferable) | |
until number_to_find == user_guess | |
puts "Try again!" | |
user_guess = gets.chomp.to_i | |
end | |
puts "You win!" |
This file contains hidden or 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
# Declare an array of the main | |
# Dungeons and Dragons Cartoon characters | |
# Index | |
# 0 1 2 3 | |
characters = ["Bob", "Sheila", "Hank", "Erik"] | |
p characters | |
p characters.sort | |
# CRUD | |
# Create: Add an element | |
# characters.push("Uni") # same as below | |
characters << "Uni" # Shovel operator / push | |
p characters | |
# Read: | |
p characters[1] | |
# Update: | |
p characters[2] # => Hank | |
characters[2] = "Diana" | |
p characters[2] # => Diana | |
# Delete: | |
p characters | |
characters.delete_at(3) | |
p characters |
This file contains hidden or 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
characters = ["Bob", "Sheila", "Uni"] | |
p characters | |
# do |character| | |
# end | |
characters.each do |character| | |
puts character | |
end | |
## Runs once for each element in characters | |
## Everytime the element will be called character | |
for character in characters | |
puts character # "Bob" / "Sheila" / "Hank" / "Erik" | |
end | |
## Iterating with while | |
characters_count = characters.count | |
iteration_count = 0 | |
until iteration_count >= characters_count | |
puts characters[iteration_count] | |
iteration_count += 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment