public
Created

random number guesser - first take

  • Download Gist
random.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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}"

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.