Skip to content

Instantly share code, notes, and snippets.

@barangerbenjamin
Created April 11, 2018 10:48
Show Gist options
  • Save barangerbenjamin/aac8fbda536a4940f1799185008de626 to your computer and use it in GitHub Desktop.
Save barangerbenjamin/aac8fbda536a4940f1799185008de626 to your computer and use it in GitHub Desktop.
# DEEFINE AN ARRAY
empty_array = [] # an empty array
beatles = ["john", "paul", "ben"] # an array of 3 elements
# GET ELEMENT BY ITS INDEX
beatles[0] # => "john"
beatles[1] # => "paul"
beatles[2] # => "ben"
# MODIFY AN ELEMENT
beatles[2] = "fish"
p beatles # => ["john", "paul", "fish"]
# ADD ELEMENT TO ARRAY
beatles << "ringo"
p beatles # => ["john", "paul", "fish", "ringo"]
# DELETE ELEMENT
#BY ELEMENT
beatles.delete("john")
#BY INDEX
beatles.delete_at(2)
#LOOP ON ELEMENTS
beatles.each do |beatle|
puts "#{beatle.upcase}"
end
nums = (0...10).to_a
for num in nums
puts num
end
puts "What time? (hour)"
print "> "
hour = gets.chomp.to_i
if hour <= 12
puts "Good morning"
elsif hour >= 20
puts "Good evening"
else
puts "Good afternoon"
end
puts "Which action [read|write|exit]"
print "> "
action = gets.chomp
# if action == "read"
# puts "You're entering read mode"
# elsif action == "write"
# puts "You're entering write mode"
# elsif action == "exit"
# puts "Exit"
# end
case action
when "read"
puts "You're entering read mode"
when "write"
puts "You're entering write mode"
when "exit"
puts "exit"
end
price_to_find = rand(5)
choice = nil
while choice != price_to_find
puts "Choose a number between 0 and 4"
print "> "
choice = gets.chomp.to_i
end
puts "You guess, it was #{price_to_find}"
# *ASSIGN* the value 28 to the variable age
age = 28
# RE-ASSIGN the value 29 to the variable age
age = 29
# VARIABLE INCREMENTATION
age = age + 1
age += 1
# VARIABLE DECREMENTATION
age = age - 1
age -= 1
# METHOD
def method_name(age)
# code here
end
puts "What hour?"
print "> "
hour = gets.chomp.to_i
if (hour > 8 && hour <= 12) || (hour > 14 && hour < 18)
puts "Open for business"
else
puts "Close"
end
puts "heads or tails?"
print "> "
user_choice = gets.chomp
computer_choice = ["heads", "tails"].sample
# if user_choice == computer_choice
# verb = "win"
# else
# verb = "suck"
# end
verb = user_choice == computer_choice ? "win" : "lost"
puts "You #{verb}"
puts "How old are you?"
print "> "
age = gets.chomp.to_i
if age >= 18
puts "You can vote"
else
puts "You can't vote"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment