Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Last active October 16, 2018 18:33
Show Gist options
  • Save rodloboz/d342ea8ff6242c54be5233ba06ca66fc to your computer and use it in GitHub Desktop.
Save rodloboz/d342ea8ff6242c54be5233ba06ca66fc to your computer and use it in GitHub Desktop.
puts "How old are you?"
age = gets.chomp.to_i
# Conditional Statements
# if / else structure
if age >= 18
puts "You can vote!"
else
puts "You're too young!! Go home!"
end
puts "Goodbye!"
puts "Pick a number?"
number = gets.chomp.to_i
# if/unless condition
# ...
# end
# inline if
puts "Your number is even" if number.even?
# unless
puts "Your number is even" unless number.odd?
puts "heads or tails?"
choice = gets.chomp
coin = ["heads", "tails"].sample
# standard if/else
# if choice == coin
# result = "winner"
# else
# result = "loser"
# end
# ternary operator
result = (choice == coin) ? "winner" : "loser"
puts "#{result}, that was #{coin}!"
hour = Time.now.hour
if hour < 12
puts "Good morning!"
elsif hour >= 20
puts "Good night!"
elsif hour > 12
puts "Good afternoon!"
else
puts "Enjoy your lunch!"
end
puts "Which action? [read|write|list|exit]"
action = gets.chomp.downcase
case action
when "read"
puts "You are in READ mode"
when "write"
puts "You are in WRITE mode"
when "list"
puts "You are in LIST mode"
when "exit"
puts "Goodbye"
else
puts "Chose a valid action!"
end
hour = Time.now.hour
condition = (hour >= 9 && hour < 13) || (hour >= 14 && hour < 19)
if condition
puts "The bootcamp is open!"
else
puts "Sorry, come back later!"
end
price_to_find = rand(5)
choice = nil
# while choice != price_to_find
# puts choice != price_to_find
# puts "How much (between $0 and $5)?"
# choice = gets.chomp.to_i
# end
until choice == price_to_find
puts "How much (between $0 and $5)?"
choice = gets.chomp.to_i
end
puts "You won! Price was #{price_to_find}"
if choice == price_to_find
puts "You won! Price was #{price_to_find}"
end
for i in (1..10)
puts i
end
for i in ["a", "b", "c"]
puts i
end
# ARRAYS
# CRUD
# Create
arr = Array.new
arr2 = [1, 4, 6]
p arr.class
p arr2
# Reading
# 0 1 2 ...
names = ["tango", "cash", "batman"]
p names
# Update
# names.push("robin")
names << "robin"
p names
# names[3].capitalize!
names.last.capitalize!
p names
# Delete
names.delete_at(3)
names.delete("Robin")
p names
# Array Methods
names.sort!
p names
# for name in names
# puts "Hello, #{name}!"
# end
# Iterator
names.each do |name|
puts "Hello, #{name}!"
end
names.each_with_index do |name, index|
puts "#{index + 1} - Hello, #{name}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment