My Stuff

2010 Conferences

OSGi DevCon @ JAX London

February 23 - Keynote titled OSGi in the Enterprise: Agility, Modularity, and Architecture’s Paradox

EclipseCon

March 22 - 25 - Tutorial on Modular Architecture

Tweets @ Twitter

Can someone explain this to me? We save our banks, but not our schools! http://bit.ly/bmYNti Why? <- Possibly I am missing something. 5 hrs ago

Just got my copy of "OSGi and Equinox: Creating Highly Modular Java Systems" by McAffer, VanderLei, and Archer. #osgi 22 hrs ago

New Blog Entry: Agile Animations - Big Teams http://bit.ly/96MP80 <- Purely animated blog entry. Feedback very welcome! 2 days ago

Apple plays architect. Will tell you what type of application you need. http://bit.ly/b5otnY 2 days ago

Macs are more expensive than PCs. But I'd argue that TCO is much lower. In general, the software you need is much less and maint is near 0. 2 days ago

LinkedIn Profile

The opinions expressed on this site are my own, and not necessarily those of my employer.

My Ruby Kata

Filed Under Development, Ruby |  

I was browsing through some older directories and stumbled across a Ruby programming kata I had done a couple of years ago. This is a simple kata where I calculate the payment for a loan based on interest rate, number of months, and loan value. I typically practice this code exercise for new languages that I’m learning, and I go through a progression of steps that help me understand the language sytanx and semantics first, before moving onto improving my design, using other features and frameworks of the language, and making it accessible via the web.

I was actually pretty surprised that the exercises still worked, save for a few scripts I had to change. The code hasn’t been touched for about two years, and I was very shocked I didn’t experience the phenomena where code that goes untouched for prolonged periods of time tends to somehow break. Strange, isn’t it? Anyway, I’ll walk you through the four versions. You can find the code for each of the versions in my Google code repository. To checkout all versions of the kata, simply do the following:

svn co http://kcode.googlecode.com/svn/trunk/kata/ruby

Version 1

It doesn’t get much simpler than Version 1. Just two files. Input.rb manages the command line interaction, and prompts for the rate, months, and value. Loan.rb performs the calculations. To run this sample, just type

ruby Input.rb

and enter values, such as 6, 60, and 15000. There is also a testcase, named LoanTest.rb.

Version 2

The second version has some minor enhancements over the first.

  • Multiple output formats - In addition to the showing the monthly payment amount for the loan, this version adds the option to also print the complete payment schedule, including principle and interest paid each month, along with the remaining balance of the loan.
  • Incorporate a test suite - I added another test for the payment schedule.

To accommodate printing the payment schedule, I modified Input.rb to prompt the user to choose to print either the payment schedule or monthly payment. I also added a new method to Loan.rb called calculatePaymentSchedule. To address the running total that I need to keep track of when calculating the full payment schedule, I also added PaymentSchedule.rb to represent the complete payment schedule. Within the PaymentSchedule instance is a collection of Payment.rb instances - one for each monthly payment.

You can run the sample in the same manner as before. Just be sure you’re in the V2 directory when executing.

Version 3

The third version is virtually identical to version 2, except here I figured I’d experiement with rake. So the only change you’ll fine here is the rakefile.rb, shown below for convenience.

require 'rake/testtask'

task :default => [:unit_test]

Rake::TestTask.new(:unit_test) do | tsk |
 tsk.libs << "./app:./test"
 tsk.test_files = "AllTests.rb"
end

There is a pretty lengthy discussion of my initial struggles with rake in the trouble.txt file included with this version. Suffice it to say that at the time I performed this kata, I had trouble finding good documentation that showed me how to do what I wanted. The only real change I’ve made recently to the rakefile was to use the PATH_SEPARATOR constant in lieu of hardcoding the separator (”:” or “;”), which I struggled with for a while when I got my Mac.

Version 4

Here, I wanted to take the code and transform it into a REST service. I wound up making it work by replacing Input.rb with Server.rb. It didn’t really wind up all that RESTful, but oh well…it works. I also added something else I’m pretty fond of, which is test coverage using RCov. My trials and tribulations with making this accessible via the web can be found in V4.txt.

To run the sample, just type the following in the terminal from within the V4 directory.

ruby Server.rb

You’ll see the server startup, then you can access the sample at http://localhost:8181/loan. In doing this, you’ll get back a response that shows you the URL parameters you’ll need to add. You can also click right here to the fully customized URL with sample parameters. Pretty simply really.

I also suggest you take a look at the rakefile.rb included with V4 because it uses rcov. As you can see from the rake tasks that have been commented out, at one time I had this working with CruiseControl.rb, though I no longer do because that bit of funtionality did succumb to the phenoma of code that isn’t exercised eventually breaks. Invoking the build with CruiseControl.rb would be a good exercise, as would the numerous other improvements that could be made to the code. But I’ve been done with this Ruby kata for about two years, and don’t intend to do anything else with it. I’m only sharing it as an example of what I like to do when learning a new language. Time for a Scala kata, Clojure kata, Fan kata, and more.

Comments

Leave a Reply