Quiz #1:
The answer key in the book has two solutions: one using ERB where you
basically convert the madlib text into an ERB template and evaluate
it. The other creates classes for Replacement object and Question
objects.
What I did was to do this without creating classes, just an array and
a hash and a string. After some playing around, I discovered that the
block form of String#gsub! was a powerful tool. Some stumbling
blocks:
1. It took me a while to discover the block form of gsub. Initially,
I used match with the additional position argument, and figured I'd
loop until the match returned nothing. Then I was going to use
String#scan.
2. Getting the structure of the block correct took some googling. I
like being able to return early from a block in order to keep the
if/else logic simple. It turns out that in ruby, you don't generally
want to use 'return', you either want 'break' or most likely, 'next'.
I just didn't realize that you can provide an argument to those
functions to control the return value out of the block.
3. Using plain 'gets' is frustrating. It tends to pick up input from
command line arguments. $stdin.gets is what you want.