Saturday, December 6, 2008

Some months with RSpec

For several months, I have been writing test-case ahead of any implementation. But the joy multiplied when we started thinking BDD. To me using RSpec as BDD tool is not much different than Rails default test mechanism other than the representation, unless you try the Story.

Test first development has a big advantage that you consider all possible scenarios before writing code. Yet sometimes you may miss some lines covered. An wonderful gem you require to check test coverage in Ruby is rcov. And no wonder RSpec has built-in support for rcov :) To test you code coverage, run
rake spec:rcov

This will create a folder named "coverage" on your rails root. Open the index.html from that folder in your browser to see how much your code is covered by test case. The screenshot is a polling script I just started.

Monday, December 1, 2008

Show numeric digit in your language

My previous post was about a Ruby on Rails plugin I wrote to convert any numeric digit to other languages' representations. Here is the internals; how that works.

That plugin converts a whole string character by character. To add any language, just add that language's digits to "NUMERIC_LOCALIZE_MAP" hash.

'bn' => ["","","","","","","","","",""],
'hi' => ["","","","","","","","","",""],
'ar' => ["٠", "١","٢","٣","٤","٥","٦","٧","٨","٩"]

def localize_numeric(source, locale_code = nil)
string_representation = source
result_string = nil

if locale_code && NUMERIC_LOCALIZE_MAP.include?(locale_code)
result_string = string_representation.gsub(/\d/){|digit|
result_string = string_representation