RubyKoans: 透過單元測試來學習 Ruby 語法

rubykoans.com/EdgeCase 所推出的 Ruby 學習工具。它的特別之處在於它是用單元測試填空題的方式來認識Ruby語法。要執行很簡單,下載之後,輸入 rake 就會開始跑測試了(如何安裝Ruby請參考這裡),例如以下的測試:


  def test_arrays_and_ranges
    assert_equal __, (1..5).class
    assert_not_equal Array, (1..5)
    assert_equal __, (1..5).to_a
    assert_equal __, (1...5).to_a
  end

其中 __ 的地方就是你要解答的部份,因為是空的,所以顯然測試不會通過(Red),它會提示你那一行出錯,然後實際的值是什麼:


~/koans] (master) .rvm-$ rake
/Users/ihower/.rvm/rubies/ruby-1.9.2-p290/bin/ruby path_to_enlightenment.rb
AboutArrays#test_arrays_and_ranges has damaged your karma.

The Master says:
  You have not yet reached enlightenment.
  You are progressing. Excellent. 20 completed.

The answers you seek...
  <"FILL ME IN"> expected but was  .

Please meditate on the following code:
  /Users/ihower/koans/about_arrays.rb:48:in `test_arrays_and_ranges'

mountains are merely mountains
your path thus far [...X______________________________________________] 20/274

當你填入答案之後,就會通過進到下一個測試。你可以看到總共有274題,要寫完也是要花點時間。這模式有趣吧,練完之後應該會對Ruby語法有扎實的認識。

參與討論

1 則留言

發佈留言

發表迴響