Skip to content

Instantly share code, notes, and snippets.

@a-leung
Created October 25, 2012 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-leung/3954194 to your computer and use it in GitHub Desktop.
Save a-leung/3954194 to your computer and use it in GitHub Desktop.
random number guesser - first take
number = rand(10)
puts 'Guess the number!'
input = 'input number'
while input != number
input = gets.to_i # keep getting more numbers
# give out hints to the user
if input > number
puts 'Too high'
end
if input < number
puts 'Too low'
end
end
puts "you got it! #{number}"
@a-leung
Copy link
Author

a-leung commented Oct 25, 2012

The if codeblocks is something I would do in C, but in a single line. Another was the input variable. In C, it's not easy to setup a memory type that can handle both integers and strings (there are tricks, but it's not pretty.) But in general, this is similar to the way I would solve the problem in C. This is nice because Ruby can handle a coding style which is similar to C.

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