OK, first I just *have* to ask: Do you have a copy of Programming Ruby? No? Get the first edition here: http://www.ruby-doc.org/docs/ ProgrammingRuby/ keep reading...! On Jun 26, 2006, at 1:27 PM, Dark Ambient wrote: > Alright, if there are any people remaining that have some patience to > repeat I'm running into the same problem again. And for now I really > want to stick to while,because now it's a real sticking point for me. > If I do nothing else in Ruby, mastering the while statement is a > must.:) > > So here I've created a method. See inline for why I think it > should work. > > #sort is the parameter for the alpha method > def alpha sort > > #setting up my arrays > s_words = [] > u_words = [] > > #assign a blank string to sort to start things off > sort = ' ' Why are you using sort as a parameter and then overwriting it here?! > > # what I'm trying to say here is that if sort remains empty > continue the loop > while sort == ' ' > # to prompt the user to enter something I've added the puts statement > puts 'Please enter a word to be sorted' > #Now I'll assign sort to whatever the user inputs. > sort = gets.chomp.downcase > #Now I want to put the string into the array. > u_words.push['sort'] u_words << sort you don't want 'sort' as that's a string with s, o, r, and t -- you want the value of the variable sort (which would be better named something like: word) > #I haven't thought it completely through but I'll probably want to > give the user a > #surrender word to stop the loop. Still I think at this point it > should loop at least > #once. Sadly, it does not, so either I'm thick or this is a hard > concept for me. #Polymorphism seems easier to grasp right now perhaps an empty word? > end > end > > TIA > Stuart instead of s_words, just use u_words.sort And while I'm at it, perhaps the method should be called "get_sorted_word_list" or something else a bit more descriptive that simply "alpha". -Rob Rob Biedenharn http://agileconsultingllc.com Rob / AgileConsultingLLC.com > On 6/26/06, Robert Dober <robert.dober / gmail.com> wrote: >> >> >> >> On 6/26/06, Dark Ambient <sambient / gmail.com> wrote: >> > Robert, >> > >> > Thank you for a great explanation. I have a question or two and >> might >> > have some more later :). The program is an assignment in a book >> > "learn to program" which uses Ruby but is pretty much for >> beginners. >> > While I've seen some of the of the other condition testers, >> (when, do, >> > case, etc) the author sticks to the real basics, while, if, else >> and >> > elsif. The author did write the program out, somewhat verbose and >> > asked that it be re-written. Suprisingly, he said while he DOES >> NOT >> > like to use "return", he might be forgiving if it was used in the >> >> >> This is a very extreme point of view, like when I learnt Pascal in >> university. >> >> > re-write. Admitedly, your code is far cleaner and more to Matz's >> > style of Ruby being almost like pseudo-code. >> > So, question 1 - "loop do" seems like a great alternative to >> "while" >> >> >> No loop do ... end or loop { } is the aequivaelent to >> while true do or while(true){ } >> it is an endless loop. >> >> > but could it be subsituted all the time when a loop is >> required ? or >> > are there times when while has some unique ways of doing things ? >> > >> > You wrote: >> > "surprisingly the example you chose, which is needed> about >> everywhere is >> > not the easiest to implement". >> >> >> > Question 2 - I'm not sure how to take this statement. To me in the >> > context of programming it seems like a pretty basic function >> > regardless of how one may master it but interested in why you chose >> > the words you did ? >> >> >> Because of the need to return from the middle of the loop >> >> > Look forward to your responses. >> > >> > Thank you again! >> > Stuart >> >> >> H''s been a pleasure. >> Cheers >> Robert >> >> > On 6/26/06, Robert Dober <robert.dober / gmail.com> wrote: >> > > Hi ambient >> > > >> > > I will answer this off-list not because it was a bad thing of >> you to >> answer >> > > on list but just because you were unlucky with the anwsers ;) >> > > >> > > First I will give you the code >> > > def ask question >> > > loop do ### this loops forever, and that's what we need >> because of >> the >> > > loosers ;) >> > > >> > > puts question >> > > case gets.chomp.downcase >> > > when "yes" >> > > return true >> > > when "no" >> > > return false >> > > else >> > > puts "Please kindly reply with \"yes\" or \"no\" >> > > end >> > > end >> > > end >> > > >> > > Now some explanations, the most important trick is to write >> your code as >> > > simple as possible, surprisingly the example you chose, which >> is needed >> > > about everywhere is >> > > not the easiest to implement, but local exits (in our case the >> "return" >> can >> > > be very helpful). >> > > >> > > So what did you want to do? >> > > Ask the user a question >> > > get a reply >> > > return true or false if the reply is syntactically correct >> > > Insult the looser ;) in any other case and do it again >> > > >> > > As there is no looping construct which tests the looping >> condition after >> the >> > > first run of the loop body it is *normal* that your code gut >> clumpsy. >> The >> > > endless >> > > loop do >> > > end >> > > or >> > > loop { >> > > } >> > > >> > > often helps to write clearer code, do not forget to break or >> return from >> > > these loops though!!! >> > > >> > > In our case we want the method to return true or false anyway >> as soon as >> the >> > > input >> > > is correct, so we kill to birds with one stone. >> > > >> > > The pseudo code above can be translated as follows >> > > >> > > loop do #### preparing for the potential need to reanswer >> the question >> ;) >> > > puts question >> > > case gets.chomp.downcase ### examine the reply >> > > ### return true or false if the code is synt. correct >> > > when "yes" ### greatinput is correct we return the >> corresponding >> > > value >> > > return true ### returning from the method gets >> us out of >> the >> > > loop to :)) >> > > when "no" ### as above >> > > return false >> > > else ### Now we get to the insult part and loop back >> to the >> question >> > > puts "Please kindly reply with \"yes\" or \"no\"" >> > > end # case >> > > end # loop >> > > >> > > Feel free 2 ask any more questions >> > > Cheers >> > > Robert >> > > >> > >> >> >> >> >> -- >> Deux choses sont infinies : l'univers et la bóŐise humaine ; en ce >> qui >> concerne l'univers, je n'en ai pas acquis la certitude absolue. >> >> - Albert Einstein >