changecase
and many others are using
Hackety Hack
to learn how to code.
Hackety Hack is a free program for Windows, Mac OS X and Linux that can teach you how to make games, applications and more and share those programs with your friends. You can ask other budding programmers questions and follow them to see what code they're working on through the Hackety Hack website.
Sign up
Learn More
secret_number = 42
# Since "ask" seems to work like "puts"
# in that it takes all input as a string,
# we need to be sure we are comparing
# apples to apples.
# Currently, secret_number is the integer
# 42. One option would be to save
# secret_number as a string:
#
# secret_number = "42"
#
# Another option would be to convert
# secret_number to a string when we compare
# it to guess below by using the ".to_s"
# method.
#
# if guess == secret_number.to_s
#
# A third option (and the one that I prefer)
# is to convert the string input to an
# integer. Compare the following 3 lines to
# the example provided in step 10 of the
# Ruby lesson.
alert "I have a secret number. take a
guess, see if you can figure it out!"
guess = (ask "Guess my number:").to_i
# You'll notice that I wrapped the ask in ()
# and used the ".to_i" method on it.
# I also moved the "ask" text to an alert
# box that preceeds the "ask". I seem to
# be experiencing a bug that cuts off the
# text rather than displaying the second
# line or wrapping a single line to the
# next line.
# If you figure out a solution to the text
# display, let me know!
if guess == secret_number
alert "Yes! You guessed right!"
else
alert "Sorry, you'll have to try again."
end