![]()
|
"http://www.w3.org/TR/REC-html40/strict.dtd">
Ruby deals with strings as well as numerical data. A string may be double-quoted ("...") or single-quoted ('...').
Double- and single-quoting have different effects in some
cases. A double-quoted string allows character escapes by a
leading backslash, and the evaluation of embedded expressions using
Ruby's string handling is smarter and more intuitive than C's. For
instance, you can concatenate strings with
Concatenating strings is much more awkward in C because of the need for explicit memory management:
But using ruby, we do not have to consider the space occupied by a string. We are free from all memory management. Here are some things you can do with strings. Concatenation:
Repetition:
Extracting characters (note that characters are integers in ruby):
(Negative indices mean offsets from the end of a string, rather than the beginning.) Extracting substrings:
Testing for equality:
Note: In ruby 1.0, results of the above are reported in uppercase,
e.g. Now, let's put some of these features to use. This puzzle is
"guess the word," but perhaps the word "puzzle" is too dignified
for what is to follow
For now, don't worry too much about the details of this code. Here is what a run of the puzzle program looks like.
(I should have done a bit better, considering the 1/3 probability of success.)
|