How Old Are You?

Posted by Doug Thu, 05 Jan 2006 18:49:22 GMT

In one of my Ruby on Rails apps, I have a User class with a dob date of birth field. My client wants me to display the user’s age.

I had added this code a very long time ago:

def age
  (Date.today - self.dob).to_i / 365.25
end

I just went back and looked at that and was surprised I had let that code stand. It was just so smelly. I poked around and didn’t find any good helpers to clean it up. So, I started asking around to the Ruby smart-guys I know. Not to name drop, but Jim Weirich is a member of XP Cincinnati. When I asked him about it he responded with this paste

That demonstrates a lot of things I like about Jim. First, he’s a super friendly guy and very accessible. I don’t remember if he gave me his AIM nick or if it’s listed on his web site. Either way, he never acts bothered by my somewhat periodic interruptions about ruby related stuff. Second, he responded with code; not just code, but code with unit tests; not just simple unit tests, but fairly exhaustive unit tests. He was able to demonstrate with those tests why my algorithm wasn’t good enough; several of his tests failed with my code.

Jim practices what he preaches. He’s a big advocate for both Ruby and Test Driven Development. Even with this simple exercise he started off with good tests. Thanks Jim! I appreciate your help.

Posted in ,  | Tags ,  | 6 comments

Copyright 2001 - 2005 by Lathi.net and Doug Alcorn

Creative Commons, Some Rights Reserved Ruby on Rails Developer Powered by Debian GNU/Linux Powered by Typo