From: Bill Guindon Date: 2006-03-05T10:00:35+09:00 Subject: Re: Simple Script Help (New to Ruby) On 3/4/06, Jordan Michaels wrote: > Hello Ruby Masters! > > I'm attempting to write a very simple guessing game script in an effort > to expand my knowledge about the Ruby Language. I'm running ruby on my > OpenSuse Linux 10 box at home. I'm typically a web-programmer, but I > want to get into something a little more deep. > > Here is my little script: > > ---------------- > # set the default result > theResult = 0 > > # create the random number > theNumber = rand(10) > > while theResult != 3 > print "Guess my number: " > theGuess = gets.to_i > > # return key > # 1 = number is lower > # 2 = number is higher > # 3 = number matched! > if theNumber < theGuess > theResult = 1 > elseif theNumber > theGuess > theResult = 2 > elseif theNumber = theGuess > theResult = 3 > end > > case theResult > when 1 > puts "My number is lower." > when 2 > puts "My number is higher." > when 3 > puts "You guessed my number!" > end This is a good place to use symbols -- if nothing else, you won't need the comments explaining the keys: if theNumber < theGuess theResult = :lower elsif theNumber > theGuess theResult = :higher elsif theNumber = theGuess theResult = :equal end case theResult when :lower puts "My number is lower." when :higher puts "My number is higher." when :equal puts "You guessed my number!" end > print "The Result: " > print theResult > print "\n" > > print "The Guess: " > print theGuess.class > print "+" > print theGuess > print "\n" > > print "The Number: " > print theNumber > print "\n" > end > ---------------- > > and here's my output when I run it: > > ---------------- > utdream@dream:~/ruby_scripts> ruby guess_number.rb > Guess my number: 1 > The Result: 0 > The Guess: Fixnum+1 > The Number: 3 > ---------------- > > The result staying at 0 tells me that NONE of my comparisons worked. Can > anyone tell me why? I'm sure this is something simple but any help would > be appreciated. > > Thanks in advance! > > Warm regards, > Jordan Michaels > Vivio Technologies > > -- Bill Guindon (aka aGorilla) The best answer to most questions is "it depends".