From: Michel Date: 2011-10-12T21:54:31+09:00 Subject: Re: Learning Ruby question regarding Looping --=-NM2/u/tF2oitYq4yU0Yh Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit "if reply == 'yes'" is not necessary because reply is always not equal to 'yes' inside the loop. Using while loops is one of the bests ways to make loops because the test to end the loop is at the beginning and so, you don't need a break statement. Instead, put all the tests next to 'while' using logicals operators if needed. You'll get a more readable code. Try this : puts 'What is your name, young man?' name = gets.chomp if name == name.capitalize puts 'Please take a seat, ' + name.capitalize + '.' else puts name + '? You mean, ' + name.capitalize + ' , right?' puts 'Don\'t you know how to spell your name?' reply = gets.chomp #start loop while reply != 'yes' puts reply + ' ? What!?' reply = gets.chomp end end > 'reply' never gets a chance to change. > > Try putting your gets for reply inside the while loop at the bottom, so the > student has a chance to change their input. > > Try this: > > #start loop > while reply != 'yes' > puts reply + ' ? What!?' > if reply == 'yes' > break > else > reply = gets.chomp > end > end > On Oct 11, 2011 8:28 AM, "Dennis Nguyen" wrote: > > > Hey there, I'm new to this forums as well as being new to Ruby. I > > decided to slowly learn ruby since I always wanted to learn a > > programming language. But I need some help to understand looping. > > > > I'm using Chris Pine's Learn to Program book and I've been playing > > around with the exercises, trying to expand the and make them do things > > beyond the original programming. But I just don't understand looping. > > > > Pine says that to make a program loop you have to give a 'while' > > condition that's always true. And then provide a way to break out of the > > loop. My sample program is a teacher asking a student his name and if > > his name isn't capitalized then she'll reprimand him. If his reply isn't > > "yes" then she'll keep repeating his reply. > > > > ------------------ > > puts 'What is your name, young man?' > > name = gets.chomp > > > > If name == name.capitalize > > puts 'Please take a seat, ' + name.capitalize + '.' > > else > > puts name + '? You mean, ' + name.capitalize + ' , right?' > > puts 'Don\'t you know how to spell your name?' > > reply = gets.chomp > > > > #start loop > > while reply != 'yes' > > puts reply + ' ? What!?' > > If reply == 'yes' > > break > > end > > end > > end > > ------------- > > > > And the result is just the What!? part being repeated over and over > > again infinitely. So clearly I don't understand loops. Any insight? > > > > And sorry if this is an obvious mistake. I studied science in school, > > not computer science or logic but I'm trying! Thanks for any help you > > guys can give! :-) > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > --=-NM2/u/tF2oitYq4yU0Yh--