As part of my Ruby learning process I started the Ruby Koans tutorial, and today I finally completed it. It only took me a couple of hours (spread over a few days) and it was really fun. Koans 177 through 184 were my favorite, they are about a “scoring project” described this way:
Greed is a dice game where you roll up to five dice to accumulate points. The following "score" function will be used calculate the score of a single roll of the dice. A greed roll is scored as follows:
- 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
The problem is really simple, but I did my best to solve it in the most “Rubyish” way possible. The final version of my implementation is this:
def score(dice)
rank = Hash. new( 0)
dice. each { | x| rank[x] += 1 }
finalScore = (rank[ 1] / 3) * 1000
finalScore += 100 * (rank[1] % 3)
finalScore += 50 * (rank[5] % 3)
rank. delete(1)
rank. keys. select{|key| rank[key] > 2}. each do |x|
finalScore += x * 100
end
finalScore
end
I think I could have made it smaller, but at the expense of readability. Any comments from the more seasoned Ruby developers out there?
Julián Hidalgo Ruby, Ruby Koans Ruby, Ruby Koans
I’m (finally) learning Ruby since a couple of days ago, and I found a great resource: the Ruby Koans. It’s basically a set of failing unit tests that you have to make pass, and by doing so you learn different aspects of the language. I wanted to edit the koans in Visual Studio using the support added by the IronRuby project, because I don’t want to pay for an editor or work in a bad one, but I wanted to run the koans with the standard Ruby executable because IronRuby’s support for Ruby 1.9 is not complete yet. And of course, I wanted to work without having to leave VS at all. Here are the steps I followed.
The first step is to create a new Visual Studio project, an IronRuby console application, and add the koans to it:

The next step is to setup an external tool to run the koans (Tools –> “External Tools…”):

When I installed Ruby using RubyInstaller I chose to add the Ruby executable to my path, so I only have to type “ruby.exe” in the command field. If the executable is not in your path you just need to specify the full route. The argument is the full path to the path_to_enlightenment.rb file, which is the one that guides you through the whole process. I marked the “Use Output window” option so I would see the output within Visual Studio. The last step is to assign a shortcut to the external tool so we can run it more easily. Go to Tools –> Options, and then to Environment –> Keyboard. In “Show commands containing” type “ExternalCommand”:

In my case I put the Ruby external tool in the first place of the Menu Contents list, so the command I’m looking for is “Tools.ExternalCommand1”:
I chose the “Ctrl + Shift + Q” shortcut, mainly because pretty much anything else was taken. Now we are all set, just hit your shortcut and begin the path to enlightenment!

This approach is also useful if you want to run your own source file (for instance, if you are typing code snippets from a book you are reading). In that case just use $(ItemFileName) (“Item File Name”) in the external tool configuration instead of “path_to_enlightenment.rb”. Again the goal is to edit the source code files in Visual Studio and run them with the latest Ruby version.
Julián Hidalgo Uncategorized Ruby, Ruby Koans