“Getting Started with Ruby” Kata

I have been having talks/ideas for some time about starting a bit of a affair with Ruby. As a .NET geek, you may be wondering “why?”.

I don’t want to “go on” too much here, if you’re curious about these, please feel free to ask:

    • It’s Designed for Programmers
    • It’s Dynamic and Not JavaScript
    • Rails Looks Interesting
    • Massive Community Movement/Involvement
    • Strong Open Source Community

I got thinking. I want to get started, but have no idea where to begin. I make no claims that I am anything other than a complete n00b.

So, I started feeling my way through the darkness and decided to try and formalise it into a kata that can be repeated.

NOTE: This is totally a “bootstrapping” kata – if you have done anything beyond the basics with Ruby, this is of no good to you!

The Kata


Create a simple class that returns a number formatted as a percentage. For whole numbers, “zero” decimal places should be removed, for fractions, they should be formatted to two decimal places.

For example:

  • “10” => “10%”
  • “10.2” => “10.20%”
  • “10.268” => “10.27%”
  • “10.00” => “10%”

When the input is less than zero or greater than 100, then the result should be capped at zero or 100.

For example:

  • “200” => “100%”
  • “-54” => “0%”

These are the rough steps I followed:

  • Install your IDE of choice (I opted for Netbeans because it is free and looked reasonable – please comment if you know better IDEs!).
  • Create your first (failing) test (I did “0” => “0%”).
  • Create a class to pass the test.
  • Add tests for more whole numbers.
  • Add tests for the range cap.
  • Add tests for two decimal place formatting.
  • Add tests for input (e.g. non-numerical strings).

I decided to stop there.

My Thoughts/Points to Note
  • The default unit test framework baked in to Ruby requires that test method names start with “test”.
  • I love the fact that unit testing is baked into the framework, and IDE’s (cleanly) support it out-of-the-box.
  • I spent AGES (like an hour) wrestling with the formatting to two decimal places. I couldn’t find any decent rounding function in the framework. My implementation is probably lame, feel free to send a pull request :P
  • I am still really getting used to the “type-less” nature of parameters etc. so not sure if I should be doing more (or even less) work with validating inputs etc. (i.e. put all our faith in duck typing, or program defensively).

You can check out my code here.
NOTE: I think this link may go down when I merge in the changes – if so, just ping me and I will update.

So, a real learning journey. Felt weird (and admittedly scary) to be sat there looking at code I didn’t really understand again. Definitely a good thing for my geek-head :)

Yes, this kata took me way longer than I had hoped/expected (*cough* ~2.5 hours).

What are your thoughts on this kata? Too little? Too much?


  1. check out rubymine (http://www.jetbrains.com/ruby/index.html) it's jetbrain's ide so think resharper but a whole ide. it's proper win :)


Post a Comment

Popular posts from this blog

GTD: Biphasic Sleep Experiment – Day 4

Privacy Online – How Much Would a “I’ll Google You” Get About You?

Visual Studio: Unknown Build Error (HRESULT 0x80131515)