# save this as guess.rb
words = ['foobar', 'baz', 'quux']
secret = words[rand(3)]
print "guess? "
while guess = STDIN.gets
guess.chop!
if guess == secret
print "You win!\n"
break
else
print "Sorry, you lose.\n"
end
print "guess? "
end
print "The word was ", secret, ".\n"
現(xiàn)在,別太擔心代碼細節(jié)了.下面是謎題程序運行的一個對話.
% ruby guess.rb
guess? foobar
Sorry, you lose.
guess? quux
Sorry, you lose.
guess? ^D
The word was baz.