From: "Jesús Gabriel y Galán" Date: 2011-05-05T17:56:29+09:00 Subject: Re: Lets play a guessing game. (how to code this better?) On Thu, May 5, 2011 at 8:45 AM, Super Goat wrote: > I am a new Rubyist. I told my friend that I was learning Ruby. He asked > me how that was going and then gave me a little challenge. His > challenge, "Write a text game that guesses numbers 0-100". My reply, > "you mean it picks a number at random, and you guess the number, it > tells you higher or lower until you get it?". Up to this point I had not > coded anything in Ruby on my own (aside from the examples in the book) > and I saw this as a great first challenge. So here is what I got. It > took me some time. I ran into trouble because I forgot to take into > consideration the Class of the variables and couldn't figure out why the > loops weren't working. > My question to you all... how could I have done this better or do you > seeing anything that is wrong.  Attached is the code. Thanks for the > feedback. > > Attachments: > http://www.ruby-forum.com/attachment/6168/100guess.rb Apart from the other comments, I would like to point out that the check about the choice being a valid number (between 0 and 100) is made only for the first choice. After the first incorrect choice, you already inside the second loop, and don't check again. My advice is to create a method that contains that logic: outputting a prompt, getting the input from the user and validating it, the code would get much cleaner: def get_valid_input # the prompt and validation logic here end target = rand(100) until (choice = get_valid_input) == target puts "Wrong, my number is #{choice < target ? 'higher' : 'lower'}" end Or something like that. Jesus.