Friday, August 5, 2011

Recently I've started teaching myself Ruby and as part of my quest I discovered the amazing Ruby Koan site by EdgeCase. The concept of a koan dates back to the foundations of Zen Buddhism. You might recognize the question "what is the sound of one hand clapping". This is actually part of a Zen koan. A koan is a question or story that serves as a fundamental teaching tool. A traditional Zen koan cannot be understood via rational thought but rather through intuition leading to an "Aha!" moment.

Ruby Koans, as developed by EdgeCase, consist of 274 test cases and incomplete Ruby code. Your task is to implement the code in order to make all the tests pass in classic TDD style. For those unfamiliar with the term, TDD means Test Driven Development. TDD follows the pattern of red, green, refactor. When implementing a new section of code you start with its tests which will all fail (be 'red') initially. Then you implement the code turning all of the tests green. Finally, and over time, you go forth with confidence and refactor the code to higher and higher levels of quality (keeping it green).

Many of the Ruby Koan tests are fairly simple to make pass. Some of the more esoteric Ruby features were more difficult to master (I thought Array.inject and blocks/yield would drive me to murder). However, one of the most interesting koan related to scoring dice throws in a fictional game called Greed. This koan is quite different than the others and is more like a Kata (a martial arts term referring to a repetitive exercise meant to hone skill -- I'll save Kata for another post). Briefly, the koan asks you to implement a class that conforms to the following requirements:

# A greed roll is scored as follows [for a roll of up to 5 dice]:
#
# * A set of three ones is 1000 points
#
# * A set of three numbers (other than ones) is worth 100 times the
# number. (e.g. three fives is 500 points).
#
# * A one (that is not part of a set of three) is worth 100 points.
#
# * A five (that is not part of a set of three) is worth 50 points.
#
# * Everything else is worth 0 points.
#
# Examples:
#
# score([1,1,1,5,1]) => 1150 points
# score([2,3,4,6,2]) => 0 points
# score([3,4,5,3,3]) => 350 points
# score([1,5,1,2,4]) => 250 points

This particular koan consists of 13 (initially failing) tests. Your implementation of the scoring method must make them all pass. Ruby, by its nature, provides a multitude of ways (more than any other language I think) to solve this problem. What follows is my first attempt. It should be noted that error checking was excluded from the requirements nor did any of the tests include input error tests (like dice rolls outside 1..6, strings, etc.).

As a student of Ruby I am keen for feedback (particularly from the Ruby masters) as this solution seems a little... wordy.

A nice article for all web designers with a lot of great tips and tricks!i recommend this to all my friends and all my members.May i recommend a Graphic maker tool every web designer should have?It;s better, faster and easier then any other tool!