Ruby Quiz wrote:

>To me, that removes a lot of the wordiness of the original code, without
>sacrificing clarity or functionality.  It's probably a touch more efficient too,
>since we trimmed quite a few operations.
>  
>
Ahhh... but:

Loaded suite test_sodoku
Started
F.F..FFFFFF.F..FF.F
Finished in 9.439033 seconds.
[verbose results snipped]
19 tests, 27 assertions, 12 failures, 0 errors

My code may be wordier, but it's also workier.  :-)

Actually the only thing wrong with your code is that you pulled a Knuth: 
"Beware of bugs in the above code; I have merely proved it correct, not 
tried it."  Your load function calls "Integet" instead of "Integer".  
Because of the rescue 0, the script never lets you know that anything's 
wrong.  Also, using an exception to handle normal behavior seems smelly 
to me, but that's a style question.

I tried tightening the rescue to only rescue the expected ArgumentError, 
and ended up with this mess:

@board << line.scan(/[\d_]/).collect {|n| begin; Integer(n); rescue 
ArgumentError; 0; end }

This works, and "Integet" correctly pukes out a NoMethodError, but it 
seems like a lot of work to clean up a syntax error that was getting 
caught (indirectly) by the unit tests anyway.  So I decided to clean it 
up by eliminating the smell coming from the rescue in the first place.  
I liked this code a lot better, as it was more "up front" about the 
'_'->'0' conversion:

# convert '_' to '0' and turn digit chars into integers.
@board << line.tr!('_','0').scan(/[\d_]/).collect {|n| Integer(n) }

A major part of this exercise for me was learning to work with 
test/unit.  I actually had a few bugs caused by my ignorance of how Ruby 
works get caught by my unit tests.  Specifically, Sodoku#copy_board got 
written after I spent an hour trying to figure out why @board = 
other.board.dup was STILL returning aliased arrays.  (Answer: @board 
*was* getting dup'ed.  But @board[0] is ALSO an array, which was 
aliased.  copy_board fixes this with a 2-level dup.)

Having unit tests allowed me to verify your refactoring, as well as 
probe my own solutions fearlessly in a language I am still coming to 
grips with.

Nifty.

-dB

-- 
David Brady
ruby-talk / shinybit.com
I'm having a really surreal day... OR AM I?