Created
October 23, 2024 14:28
-
-
Save dedemenezes/636a72c6fab8345c632e0f523c72b332 to your computer and use it in GitHub Desktop.
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
# RECAP! | |
# Data type | |
# String | |
"Le Wagon" | |
# Integer | |
12 | |
# Float | |
12.2 | |
# Booleans | |
true | |
false | |
# Range | |
(1..10) # includes the last element | |
(1...10) # DOES NOT include the last element | |
# Array | |
[] | |
['jana', 'bia', 'nominoe'] | |
name = 'Andre' | |
puts name |
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
# if CONDITION | |
# code if truthy | |
# end | |
puts 'How old are you?' | |
age = gets.chomp.to_i | |
condition = age >= 16 | |
if condition | |
puts 'You can vote' | |
else | |
puts "You can\'t vote" | |
end | |
# if !(age >= 18) | |
unless age >= 18 | |
puts "You cannot drive!" | |
else | |
puts "You can drive!" | |
end |
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
# Ternary Syntax | |
# CONDITION ? code_if_truthy : code_if_falsey | |
computer_coin = ['heads', 'tails'].sample | |
puts "heads or tails?" | |
guess = gets.chomp | |
verb = guess == computer_coin ? 'won' : 'loose' | |
puts "You #{verb}" |
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
puts 'What time is it? (hour)' | |
hour = gets.chomp.to_i | |
# 24 hour clock | |
if hour < 12 | |
puts 'Good morning!' | |
elsif hour < 18 | |
puts 'Good afternoon!' | |
elsif hour >= 18 | |
puts 'Good evening!' | |
end |
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
puts 'What do you want to do? [read|write|exit]' | |
action = gets.chomp | |
# case action | |
# when 'read' | |
# puts 'Entering the READ mode' | |
# when 'write' | |
# puts 'Entering the WRITE mode' | |
# when 'exit' | |
# puts 'See you soon! zo/' | |
# else | |
# puts 'Wrong action...' | |
# end | |
case action | |
when 'read' | |
puts 'Entering the READ mode' | |
when 'write' | |
puts 'Entering the WRITE mode' | |
when 'exit' | |
puts 'See you soon! zo/' | |
else | |
puts 'wrong action' | |
end |
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
# OPEN FROM 9 to 12 | |
# CLOSE FROM 12 to 13 | |
# OPEN FROM 13 to 18 | |
puts 'What time is it? (hour)' | |
hour = gets.chomp.to_i | |
if (hour >= 9 && hour < 12) || (hour >= 13 && hour < 18) | |
puts 'STORE IS OPEN!' | |
else | |
puts 'Store is CLOSED!' | |
end |
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
price = rand(1..5) | |
puts 'What is the price? (from $1 to $5)' | |
guess = gets.chomp.to_i | |
# while guess != price | |
# puts 'What is the price? (from $1 to $5)' | |
# guess = gets.chomp.to_i | |
# end | |
until guess == price | |
puts 'What is the price? (from $1 to $5)' | |
guess = gets.chomp.to_i | |
end | |
puts "You won! The price was #{price}" |
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
empty_array = [] | |
students = ['hermione', 'ronald', 'draco'] | |
# index 0 1 2 | |
# READ | |
puts students[0] | |
puts students[1] | |
# students[array.length - 1] == students[-1] => both return the last elements | |
puts students[-1] | |
# MODIFY/UPDATE | |
students[2] = 'harry' | |
# APPEND a new element | |
students << 'luna' | |
students.push('luna') | |
students.insert(1, 'luna') | |
# DELETE/REMOVE an element | |
students.delete('luna') | |
# students.delete_at(0) | |
# SIZE/LENGTH | |
puts students.length | |
puts students.size | |
# LOOP EACH | |
p students | |
students.each do |student| | |
puts student | |
end | |
# CRUD | |
# Create << || push(element) | |
# Read [index] | |
# Update [index] = new_value | |
# Delete .delete(element) || .delete_at(index) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment