Saturday, July 28, 2007

Turn off color in output of ls command

Output of ls command in color mode sometime causes a problem to my eyes. Turn it off is simple, just put
unalias ls
in .bash_profile or .bashrc (in case of Ubuntu Feisty)

Thursday, July 26, 2007

Fixture path in ActiveRecord Testing

I am using fixture in ActiveRecord Testing. I have many test classes, each using fixtures located in different directories. Individual test work fine, but fails when running together in suite. The problem is fixture_path is class attribute/variable of Test::Unit::TestCase so all subclasses of it share the same value.
require 'rubygems'
require 'active_record'
require 'active_record/fixtures'

class TestFoo < Test::Unit::TestCase
   self.fixture_path='/u01/test/fixtures/foo'
end

TestFoo.fixture_path # "/u01/test/fixtures/foo"

class TestBar < Test::Unit::TestCase
   self.fixture_path='/u01/test/fixtures/bar'
end

TestBar.new().fixture_path # "/u01/test/fixtures/bar"
TestFoo.new().fixture_path # "/u01/test/fixtures/bar"
Because the Test Unit framework, load all test classes, then run them, so TestFoo.new().fixture_path get value of TestBar.new().fixture_path. The solution is simple, just define a method fixture_path in each test class (in upcoming version after ActiveRecord 1.15.3, the problem is fixed by change fixture_path to class instance variable see ticket 6672).
class TestFoo < Test::Unit::TestCase
   def fixture_path
      '/u01/test/fixtures/foo'
   end
end

class TestBar < Test::Unit::TestCase
   def fixture_path
      '/u01/test/fixtures/bar'
   end
end

TestBar.new().fixture_path # "/u01/test/fixtures/bar"
TestFoo.new().fixture_path # "/u01/test/fixtures/foo"

Wednesday, July 18, 2007

Exception handling

I have seen a lot of code, that handle exception in the following way
begin
  do_some_things()
rescue
  #... print message, closing file/socket etc. 
end
There is a problem with this style. If there is error, we lost information where it happens because it is quite common that do_some_things() again call methods of others classes located in different files and so all.
I think that it should be a rule, that we alway print/save to logfile backtrace of exception in every exception handling code
begin
  do_some_things()
rescue => e
  require 'pp'
  pp e
  pp e.backtrace
  #... print message, closing file/socket etc. 
end
The backtrace information will help us to identify bug during development and to analyze root cause of exception during operation.

String concatenation

One of common thing that any programs do is concatenate two or more String value, e.g.
first_name = 'huy'
sir_name = 'le'
name = first_name + ' ' + sir_name # huy le
But the above code suffers a problem, if one of concatenated variables is nil, then it will not work. Checking nil is tedious and error prone, a simple solution of this problem is putting concatenated variables into an array, removing nil using compact and using join method to perform concatenation.
first_name= 'huy'
middle_name=nil
sir_name = 'le'
name = [first_name, middle_name, sir_name].compact.join(' ') # huy le

Sunday, July 1, 2007

Ruby block and command/query separation

The recent post Ruby block style do end versus {} on where to use do ... end versus {...} to enclose Ruby block reminds me command/query separation OOP principle.