Ruby handles strings like numbers. We enclose them in single quotes (' ... ') or double quotes ("...").
Ruby> "ABC"
"ABC"
Ruby> ' abc '
"ABC"
Single and double quotes have different effects in some cases. A string enclosed in double quotes allows characters to be drawn by a forward slash, and can be inline with #{} expressions.
Strings that are enclosed in single quotes do not interpret strings; What you see is what it is. Several examples:
Note: In Ruby 1.0, the above results appear in uppercase letters.
OK, let's try these features. Here is a riddle of guessing words, maybe the word "riddle" is so cool in the following things;--)
# 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"
Now, don't worry too much about the code details. Here is a dialog for the puzzle program to run.
% Ruby Guess.rb
Guess? Foobar
Sorry, you lose.
Guess? Quux
Sorry, you lose.
Guess? ^d
The word was Baz.
(Given the 1/3 success rate, maybe I should have done a little better.)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.