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.

One thought on “Ruby Koans”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>