Skip to content

Instantly share code, notes, and snippets.

@marcoranieri
Created October 9, 2019 17:57
Show Gist options
  • Save marcoranieri/527915035ecd00cb55db2b6230d92dc4 to your computer and use it in GitHub Desktop.
Save marcoranieri/527915035ecd00cb55db2b6230d92dc4 to your computer and use it in GitHub Desktop.
LIVECODE_Flow_conditional_array
def acronymize(sentence) # sentence => "be right back"
return "" if sentence == ""
# split sentence into array
splitted = sentence.split #=> ["be", "right", "back"]
# iterate .each and store first letters
first_letter = [] # before iteration
splitted.each do |word|
first_letter << word[0]
end
# with the FOR loop
# for word in splitted
# first_letter << word[0]
# end
# first_letter = ["b", "r", "b"] after iteration
# join them and upcase
result = first_letter.join.upcase
# return IT
return result
end
puts acronymize("") == ""
puts acronymize("be right back") == "BRB"
puts acronymize("be right back") # you can puts the result
brb = acronymize("be right back") # or store the result
OPTIONS = %w(paper rock scissor)
# take a random option for computer
computer_choice = OPTIONS.sample
#puts "Computer: #{computer_choice.upcase}"
# prompt the user to choose an option (check it)
puts "What is your choice? #{OPTIONS.join(" ")}"
user_choice = gets.chomp
if OPTIONS.include?(user_choice)
# compare!
# outcome: win lose draw
if computer_choice == user_choice
puts "Draw!"
else
if computer_choice == "paper" && user_choice == "scissor" ||
computer_choice == "rock" && user_choice == "paper" ||
computer_choice == "scissor" && user_choice == "rock"
puts "You win!"
else
puts "You lose!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment