Skip to content

Instantly share code, notes, and snippets.

@jomontanari
Created October 28, 2011 12:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jomontanari/1322183 to your computer and use it in GitHub Desktop.
Save jomontanari/1322183 to your computer and use it in GitHub Desktop.
Ruby - Day 1 - bonus problem (7l7w)
puts "What's the maximum number?"
max_num = gets().to_i
number_to_guess = rand(max_num + 1)
puts "Thinking of a number between 0 and #{max_num} ... OK."
guess = nil
while guess != number_to_guess do
puts "Please make a guess ..."
guess = gets().to_i
puts "Too high!" if guess > number_to_guess
puts "Too low!" if guess < number_to_guess
end
puts "Well done! You got it!"
@philosodad
Copy link

Same comment as on Mary-annes code, you can eliminate the guess assignment by writing
while (guess ||= nil) != number_to_guess do

The trailing 'if' clause feels more rubyish than an if...elsif statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment