-
-
Save richardharrington/97b7c47de07f0b41fb06 to your computer and use it in GitHub Desktop.
random number guesser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num = rand(10) | |
print "Pick a number: " | |
guess = gets.to_i | |
until num == guess | |
guessType = guess > num ? "high" : "low" | |
puts "Your guess is too #{guessType}." | |
print "Guess again: " | |
guess = gets.to_i | |
end | |
puts "You got it, my friend." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num = rand(10) | |
print "Pick a number: " | |
begin | |
guess = gets.to_i | |
unless num == guess | |
guessType = guess > num ? "high" : "low" | |
puts "Your guess is too #{guessType}." | |
print "Guess again: " | |
end | |
end until num == guess | |
puts "You got it, my friend." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num = rand(10) | |
print "Pick a number: " | |
until (guess = gets.to_i) == num | |
puts "Your guess is too #{guess > num ? "high" : "low"}." | |
print "Guess again: " | |
end | |
puts "You got it, my friend." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The file rand_take1.rb above is close to my original try at the random number guesser in Ruby.
After the Ruby meetup, Andrew Leung mentioned that he did this exercise first in a more C-like style, then tried to do it more in a Ruby-like style (here are his original and revised gists).
I'm not sure whether my three takes lead to it being more Ruby-like, but they came out of Andrew's challenge to me to see if I could get rid of the double "guess = gets.to_i" line, which is in the version above. The second try is rand_take2.rb
rand_take2.rb accomplishes its goal by the use of Ruby's begin...end construct, and possibly makes it easier to read, but I don't like it because a) it seems just as verbose as the first one, b) I don't like the "end until " idiom, and c) although it has gotten rid of the repeated "gets", it has now added a repeated test of whether num == guess, with every loop iteration.
So, based on Andrew's use of the assignment-as-expression in the middle of the "until" loop test, I have gone back mostly to my original version but made it a lot shorter. (I also got rid of the 'guessType' variable. That was a judgment call, but I think it's not too terse.)
Andrew's final comment was about how readable Ruby's postfix 'if' statements are. For me, the thing that most interests me about my Ruby code, that I might not be able to do in other languages, is to eliminate all the negation in tests by using the English words 'unless' and 'until' instead of 'if not' and 'while not' (or 'if !' and 'while !'), respectively. It took me a while to get used to 'unless' and 'until', but I'm beginning to think they improve the code a lot.