Last active
December 14, 2015 23:39
-
-
Save ejallday/5167671 to your computer and use it in GitHub Desktop.
Chirs Pine 8.3 Excercise
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
| ## Write a program that asks for as many word as you want (one word per line) | |
| ## until you press enter on an empty line, and then repeats all the words back in alphabetical | |
| ## order | |
| ## Where to being with this one? I got to the words = [] portion but wasn't | |
| ## sure how to get multiple words, entered separately a.) into the array and b.) on a loop. | |
| ## After trying a variety of ways, I broke down and did some googling. | |
| ## I came up with this... Explanations to follow. | |
| puts 'Please type one word on each line and hit enter...' | |
| puts 'When you get bored, simply hit enter on a blank line.' | |
| words = [] | |
| word = gets.chomp | |
| while word != '' | |
| words.push word | |
| word = gets.chomp | |
| end | |
| puts words.sort | |
| ## breaking this code down by line... words = [] simply establishes a new array | |
| ## word = gets.chomp defines the variable 'word' and asks for input via the gets method and turns the | |
| ## inputs into strings. These strings are then put through the 'while' filter... | |
| ## while word!='' ... Essentially what its saying to me is that as long as the input for the variable 'word' | |
| ## does not equal a blank (!='') --> take the 'word' variable and push it to the end of the 'words' array | |
| ## (words.push word) | |
| ## if the input was not a blank, the program automatically pushes it into our array... so, | |
| ## at this point, we need the program to ask us for another input for the 'word' variable | |
| ## hence (word = gets.chomp) the second time. | |
| ## if at anytime the input is blank, the program ends and displays a sorted verision of all the | |
| ## strings that were pushed into the array. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment