From: RoRNoob Date: 2008-04-17T02:30:06+09:00 Subject: Re: Noob Questions [arrays] On Apr 16, 9:55 am, RoRNoob wrote: > On Apr 16, 3:25 am, Ron Fox wrote: > > > > > Approach it as follows; > > 1. A loop that is not a .each loop that reads in user input > > until the input is an empty line (that is size 0 after being chomped) > > 2. Then sort thearray. > > 3. Then a .each iteration through thearrayto output the contents. > > > RF > > > RoRNoob wrote: > > > Be warned - I'm new to Ruby and programming in general, so I'm sure > > > this will be a trivial question for most of you. > > > > I'm currently working through the exercises in the book Learn to > > > Program by Chris Pine. At the end of each chapter Chris has a little > > > programming problem that the reader is asked to solve. I've been able > > > to follow along just fine up until tonight. > > > > The problem in question is at the end of Chapter 8 (Arrays and > > > Iterators): I have to write a program that asks the user to type in as > > > many words as they want (one word per line, continuing until they just > > > press Enter on an empty line) and then repeat the words back to them > > > in alphabetical order. > > > > The program lives in a .rb file and is executed and ran from the > > > terminal. > > > > Here is my code: > > > > alphalist = [] > > > alphalist.each do |item| > > > puts 'type words one at a time and I will alphabetize them for you.' > > > alphalist.pushgets.chomp > > > end > > > puts alphalist.sort > > > > When I run the .rb file, nothing happens. Any help is appreciated! > > Cool - I will give that a try - thanks for your time everyone! OK progress: alphalist = [''] input = '' while input != 'bye' puts 'type words one at a time and I will alphabetize them for you.' input = gets.chomp if input != 'bye' alphalist.push input end end puts alphalist.sort ----- My problem is that I can't figure out how to make it exit the loop if the user submits a 0 length string. The only way I've been able to get it to work is to watch for 'bye' - which I like better than the empty string, but the problem from the book calls for an empty string loop exit