Geoff
7/26/2006 2:12:00 PM
I think I like this one the most. It's closest to what I had in my head
as an ideal solution. I just didn't know how to use the empty? method.
Also, in my while loop I needed to initialize the input variable before
the loop, but in the 'loop do' you use here it does not need to be.
Would you mind explaining why?
Thanks again,
Geoff
Morton Goldberg wrote:
> You're very close to what is needed. The following variation on your
> code will do the job.
>
> Regards, Morton
>
> #! /usr/bin/ruby -w
>
> array_input = []
> loop do
> input = gets.chomp
> break if input.empty?
> array_input << input
> end
> puts
> puts array_input.sort
>
> On Jul 26, 2006, at 12:55 AM, Geoff wrote:
>
> > "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.