Tag Archives: koans

… and the Student Was Enlightened

As a huge fan of the ancient Tao of Programming text, I was happy to see someone returning to the format with a more modern programming spin.

The Abbess Jinyu was inspecting the state of the Laughing Monkey clan. In the hall she came upon a half-drowned monk, soaked and shivering.

“Explain,” said the Abbess.

“The m-m-master commanded me to t-take his coracle out onto the r-r-river,” said the monk through chattering teeth. “When I was far from shore I discovered that it had m-m-many holes in the sides. M-most were plugged with corks but t-t-t-two had been overlooked. Had I not stopped them with my f-f-fingers I would have p-perished.”

The Abbess bowed and went in to see the Java master.

“Explain,” said the Abbess, indicating the monk.

“That monk was given the task of implementing the Shopping Cart for our new site,” said the master. “He discovered that if a particular database entry was not initialized, attempts to display the Shopping Cart would fail. So the monk found every page that offered a link to the Shopping Cart and attempted to initialize the database entry there. There were over a dozen such pages. Lamentably, two were overlooked.”

The Abbess struck the master with her cane.

“Next time,” she said, “Drill more holes.”

More thought and smile provoking koans can be found at The Codeless Code

Ruby Koans

Ben sent me a link to Ruby Koans today. These “Koans” are a kind of learn-by-doing approach to learning ruby.

Each problem is set up as part of a test-driven framework that requires you to make the tests pass by filling in the answers.

An example:

def test_combining_hashes
  hash = { "jim" => 53, "amy" => 20, "dan" => 23 }
  new_hash = hash.merge({ "jim" => 54, "jenny" => 26 })

  assert_not_equal hash, new_hash
    
  expected = { "jim" => __, "amy" => 20, "dan" => 23, "jenny" => __ }
  assert_equal expected, new_hash
end

This was a great way to spend a couple of hours and learn some of the deeper secrets of the ruby language.

Oh, and if you have a problem running the problems in 1.9.1, specifically the following:

C:\ruby_koans>rake
(in C:/ruby_koans)
cd koans
C:/Ruby19/bin/ruby.exe path_to_enlightenment.rb
C:/ruby_koans/koans/edgecase.rb:33:in `<class:Sensei>': uninitialized constant T
est::Unit::AssertionFailedError (NameError)
        from C:/ruby_koans/koans/edgecase.rb:30:in `<module:EdgeCase>'
        from C:/ruby_koans/koans/edgecase.rb:29:in `<top (required)>'
        from C:/ruby_koans/koans/about_asserts.rb:4:in `require'
        from C:/ruby_koans/koans/about_asserts.rb:4:in `<top (required)>'
        from path_to_enlightenment.rb:3:in `require'
        from path_to_enlightenment.rb:3:in `<main>'
rake aborted!
Command failed with status (1): [C:/Ruby19/bin/ruby.exe path_to_enlightenme...]

(See full trace by running task with --trace)

You need to run:

gem install test-unit

The test-unit gem is no longer included in the Ruby installer.