By Developers, For Developers

Historical errata for Best of Ruby Quiz

PDF PgPaper PgTypeDescriptionFixed onComments
116-7SUGGEST

In the “unusual construct”, “method({key => value})”, the braces are unnecessary; the named-argument style “method(key => value)” should be used.

138-9ERROR

There are three lines in my code where I “throw” exceptions instead of using “raise” or “fail”. It will cause an error, just not the intended one. That’s a mistake you can easily make coming from Java.

215TYPO

In the 3rd paragraph of the comment above “class Maze”:

If 1 one is set, then p has a north

  1. wall; if bit 2 is set, then p has a west wall.

The original version was (from http://www.rubyquiz.com/quiz31.html):

If bit one is set then p has a north wall, if

  1. bit two is set then p has a west wall.

The corrected version probably should be:

If bit 1 is set, then p has a north

  1. wall; if bit 2 is set, then p has a west wall.
62ERROR

if the solution “erb_madlibs.rb” should also work with placeholders which run over multiple lines like in example “Gift_Giving.madlib”, the reg expression “/\\(\\(\\s\\s\\)\\)/” should have at least a multiline option e.g. “/\\(\\(\\s\\s\\)\\)/m”

67TYPO

“… . Otherwise, the sub( ) call will fail, and $1 will be unaltered. … ”
As I can see $1 will get Nil and the assignment of the user input to h[$1] won’t be relevant for further hash calls in the gsub block.

169ERROR

Method Cipher.normalize is incorrect.

The easiest way to verify this is to add the following test to tc_cipher’s test_normalize.

assert_equal( “WELCO METOR UBYQU IZXXX”,
Cipher.normalize(“Welcome to ruby quiz.”) )

This will cause a failure:

test_normalize(TestCipher) [./tc_cipher.rb:34]:
<“WELCO METOR UBYQU IZXXX”> expected but was
<“WELCO METOR UBYQU”>.

The problem is with line two of Cipher.normalize:
text += (“X” * (text.length % 5))

This code adds only (text.length%5) number of “X”s, which is not enough if text.length is 1 or 2. You could fix this by correctly computing the number of X’s necessary to pad out to a multiple of 5, or take my more brute force approach of replacing this line with the following:

text += “XXXX”

Since 4 is the maximum number of X’s that could be needed, and the extras will harmlessly be thrown out by the following line:

text.scan(/.{5}/).join(" ")

Hope that is helpful (and not too pendantic).
-CS

72SUGGEST

A very minor issue concerning the usage conditions for erb_madlib.rb.

I suggest checking that the file extension name for the madlib input file is .madlib by adding a File.extname(ARGV[0]) == “.madlib” to the usage unless conditions.

Currently, if the file is, say, Lunch Hungers.madlip, and that file exists, when the story prints out, the title at the top of the screen will be Lunch Hungers.madlip instead of just Lunch Hungers (the File.basename(ARGV.first, ‘.madlib’) condition fails).

2rand2randERROR

+z$8frZdyL%68pSU/:>w<:E3.lG-!XIB

Categories: