Archive for the 'Programming' Category

The sublime joy of bug-fixing

Friday, September 26th, 2008

Find a bug. Write a test to reproduce it. Fix it. Watch the test pass. Check in the changes.

Realize you’re one step closer to perfection.

How to reload a class in irb

Tuesday, September 16th, 2008

I’m working interactively in irb, and I tweak the class to make changes. The changes won’t be reflected in the irb session unless you reload the class.


irb >> load 'document.rb'

Make sure to add the file extension. Unlike require, load needs the full filename.

How to test tag attributes using assert_select

Wednesday, September 10th, 2008

There’s undoubtedly a smarter way to do this, but I couldn’t figure it out. I’m doing a functional (controller) test for one of my rails pages. I want to make sure that the picture coming back is exactly 224 pixels high.

I couldn’t find a way to do this using the assert_select syntax, but this workaround seems to work:


  def test_picture_height
    assert_select "div.secondary div.lead_picture img[height]" do |height|
      # pull the tag -- this gives you the whole img tag
      @h = height.to_s
      # extract the height
      @h.gsub!(/.*height="(.*?)".*$/, '\1')
      #run the test
      assert_equal @h, "224"
    end
  end

This can be tightened up, but I left it explicit so that I could understand what I was going. The assert select makes sure that I’m setting the height. The block makes sure that it’s equal to 224.

Are there easier ways to do this?

How to get Subversion to ignore your Rails log files

Friday, September 5th, 2008

Rails puts the log files in the same part of the tree that ends up under source control. If you don’t tell Subversion to ignore them, you’ll end up with all the log files under source control, which will drive you nuts.

If you already have them in the system:


cd YOUR_RAILS_APP/log
ls -l # Make sure you're in the right directory
svn --force rm * # Force Subversion to kill those files
cd .. # Move back to the rails root level
svn propset svn:ignore "*.log" log

Hope this helps.

Revving the prototypes

Monday, August 25th, 2008

I just started a new Subversion repository, taking to heart Fred Brooks’ advice to “Build one to throw away.”

Of course, I’m planning on building four or five to throw away. If things go extremely well, I’ll have something release-worthy by prototype six…