Last active
December 29, 2015 06:39
-
-
Save hchood/7630357 to your computer and use it in GitHub Desktop.
Alpha checkpoint: Echo (Phase 4)
This file contains hidden or 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
# Alpha: Echo checkpoint (Phase 4) | |
# METHODS | |
def playback(input) # This method is now way too long. Way to make it shorter? Or just move out of method into main program? | |
if input == "Nothing!" | |
"Ok, fine!" | |
elsif input == "I have a lot to say" | |
puts "Ok, let's hear it!" | |
multiline_output_from(gets_lines_of_input) | |
elsif input == "I have something prepared" | |
puts "OK, where can I find what you want to say?" | |
filename = gets.chomp | |
until File.exists?(filename) | |
puts "I couldn't find that file.\nWhere can I find what you want to say?" | |
filename = gets.chomp | |
end | |
puts "Loading #{filename}..." | |
lines_of_input = gets_input_from_file(filename) | |
multiline_output_from(lines_of_input) | |
else | |
"You said: #{input}" | |
end | |
end | |
def gets_lines_of_input | |
lines_of_input = [] | |
input = nil | |
until input == "done" | |
input = gets.chomp | |
lines_of_input << input unless input == "done" | |
end | |
lines_of_input | |
end | |
def gets_input_from_file(filename) | |
file = File.read("#{filename}") | |
lines_of_input = file.split("\n") | |
end | |
def multiline_output_from(lines_of_input) | |
num_lines = lines_of_input.length | |
first = "You said: #{lines_of_input.shift}\n" | |
last = "Finally you said: #{lines_of_input.pop}\n" | |
middle = lines_of_input.map { |line| "Then, you said: #{line}\n" }.join | |
first + middle + last + "Phew! Glad you got all #{num_lines} of those things off your chest!" | |
end | |
# PROGRAM | |
puts "What do you want to say?" | |
input = gets.chomp | |
puts playback(input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment