How to convert a string to UPPERCASE or lowercase in Ruby?
In this exercise, you were asked to create the first version of the Number guessing game. We are going to see a solution in Ruby.
print "Type in your guess: "
puts "Your guess is smaller that the hidden number"
puts "Your guess is bigger that the hidden number"
We use the rand function to generate a random number. If we called it without any parameter, rand would return a floating point number between 0 and 1. As we passed a whole number to it, the rand function generated a whole number between 0 and 200, the number we gave.
There is a commented out line printing the hidden value back. I used it to be able to test the code which was comparing my input to the hidden number.
Then we use the print function to ask the user to type in a number. We use the print functions instead of the puts function to avoid adding a trailing newline. So the cursor will appear on the same line where the request was printed.
Then we have 3 cases depending the relative values of the hidden and guess variables.