Geoff wrote: > Hey, > > I've searched the archives and don't see anything on this problem for > Chris' book. From Chapter 7 on Arrays and Iterators: > > "Write the program we talked about at the very beginning of this > chapter. > Hint: There's a lovely array method which will give you a sorted > version of an array: sort. Use it!" > > >From the beginning of the chapter: > > "Let's write a program which asks us to type in as many words as we > want (one word per line, continuing until we just press Enter on an > empty line), and which then repeats the words back to us in > alphabetical order." > > Here is what I have that works, but I had to change it in order to get > the program to exit. You see I can't seem to get the program to exit > on just entering a blank line at the prompt, so I wrote some code so > that it would quit with when I entered the word "bye." > > array_input = [] > input = '' > while input != "bye" > input = gets.chomp > array_input << input > end > puts > array_input.delete_if { |x| x == 'bye' } > puts array_input.sort > > Problem is I would actually like to know how to solve the problem as > written! So when I use "while input != "something goes here and I'm > not sure what to use to designate a blank line." Then I can eliminate > the second to the last line. > > Thanks! > > Geoff array_input = [] while (input = gets.chomp) != "" array_input << input end puts puts array_input.sort Or: array_input = [] loop do input = gets.chomp break if "" == input array_input << input end puts puts array_input.sort You could leave the "\n" at the end of each line. However, if you forget about the "\n", it may cause you lots of trouble. array_input = [] loop do input = gets break if "\n" == input array_input << input end puts puts array_input.sort