The Ruby Learning Blog continues to sizzle with one amazing blog post after another everyday by accomplished rubyists! Today’s post by Noel Rappin was of particular interest to me as I had attempted to solve A Quick Ruby Kata he has used in this post as an example, and had posted on his blog a couple of days ago. The Kata was similar to a Project Euler problem I had solved earlier; so had modified to code for the Project Euler problem to solve the Kata.
The way I went about solving the problem was like with any other Project Euler problem I have solved so far : start writing code to solve the problem and use puts statement frequently to validate the code I have written; and as soon as I am able to get a clear picture of the problem, complete the code for the full program, let it run and get the answer (when it comes).
In this blog post, Noel Rappin details how the problem can be solved with having a testing mindset; conceiving and implementing the test cases first using RSpec, and then coding the functionality - this couple of steps are repeated multiple times till the problem is solved. Terrific illustration of how to break up the problem into smaller parts and address them TEST-FIRST!.
Quite a few learning points in the blog post for me:
- First and foremost is of course that Test Driven development can be easily incorporated into the development cycle; I don’t have to go way out of my way to do that. In fact, by breaking down “a task down into tiny, component pieces" and writing the tests for each of these component pieces, I think I am likely to arrive at the solution quicker, and have a better grasp of the problem and the coding done so far. Will take on this practice enthusiastically for my development work from now on.
- I had done a little bit of work with RSpec earlier - having just installed the RSpec gem a couple of days ago; the rspec examples in this blog post makes me more comfortable with the usage of RSpec.
- The usage of Enumerator class in Ruby 1.9
- Class Methods in Ruby - Came across them for the first time in this example! ( Two ways of declaring a class method are illustrated in the example 1. def self.sequences … end and 2. class « self def sequences … end end )
- A couple of other idioms new to me in this example : 1. @histogram ||= begin (while I knew about the ||= before, giving a block to it as the param is new to me) 2. Enumerator.new do |y| (applying a do loop to the new method is new to me) 3. if __FILE__ == $0 check to ensure execution only when the file containing the code is executed, and not when the class is invoked from other files (including test files).
OK, now I know the usage of TestUnit and RSpec; Cucumber is next! And no excuse to not do TDD from now on.