Skip to content

Instantly share code, notes, and snippets.

@marcoranieri
Created February 19, 2020 09:58
Show Gist options
  • Save marcoranieri/ac4fd77c75190a66c4574f4949332d57 to your computer and use it in GitHub Desktop.
Save marcoranieri/ac4fd77c75190a66c4574f4949332d57 to your computer and use it in GitHub Desktop.
Flow & Array
cities = %w(rome paris perugia)
# ["rome", "paris", "perugia"]
# index 0 1 2
# -3 -2 -1
# READ
cities[1]
# CREATE
# cities.push("New York")
cities << "New York"
# UPDATE
cities[2] = "london"
# DELETE
cities.delete_at(3)
# cities.delete("New York")
p cities # puts cities.inspect
# _CRUD_
# READ [index]
# CREATE <<
# UPDATE [index] =
# DELETE delete_at(index)
puts "What time is it?"
hour = gets.chomp.to_i
if hour < 12
puts "Good morning"
elsif hour > 20
puts "Good evening"
elsif hour > 12
puts "Good afternoon"
end
puts "Which action?"
action = gets.chomp
# if action == "read"
# puts "Read mode"
# elsif action == "write"
# puts "Write mode"
# elsif action == "exit"
# puts "Exiting.."
# else
# puts "Invalid input"
# end
case action
when "read" then puts "Read mode"
when "write" then puts "Write mode"
when "exit" then puts "Exiting.."
else
puts "wrong input"
end
computer = rand(5)
guess = nil
# until guess == computer
while guess != computer
puts "Try again"
guess = gets.chomp.to_i
end
puts "You WIN!"
puts "What time is it?"
hour = gets.chomp.to_i
is_morning = hour >= 9 && hour < 12
is_afternoon = hour >= 15 && hour < 19
if is_morning || is_afternoon
puts "It's open!"
else
puts "It's close.."
end
# puts "How old are you?"
# age = gets.chomp.to_i
# if age >= 18
# puts "you can vote"
# else
# puts "you cannot vote"
# end
# # condition ? true_block : false_block
# answer = age >= 18 ? "you can vote" : "you cannot vote"
# puts answer
# HEADS or TAILS
computer = ["heads", "tails"].sample
puts "Heads or Tails?"
answer = gets.chomp
outcome = answer == computer ? "Win" : "Lost"
puts outcome
# number.even? ? "EVEN" : "ODD"
puts "How old are you?"
age = gets.chomp.to_i
# if !age >= 18
unless age >= 18
puts "you can vote"
else
puts "you cannot vote kiddo!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment