From: Brian Adkins Date: 2007-03-15T01:45:06+09:00 Subject: Re: Calculating the age given DOB Reid Thompson wrote: > On Wed, 2007-03-14 at 23:55 +0900, Deniz Dogan wrote: >> Hello, fellow rubyists. >> >> I have a problem. I'm trying to write a simple program which calculates >> the age of a person given the person's date of birth in an instance of a >> Date class. I have no idea what is the best way to do this. I don't feel >> like using any ugly "hacks" to do it, and I wonder if there is any good >> (perhaps mathematical) algorithm out there to solve the problem. >> >> --Deniz Dogan >> > rthompso@jhereg:~$ irb > irb(main):001:0> require 'date' > => true > irb(main):002:0> bd = Date.new(1966, 1, 21) > => # > irb(main):003:0> today = Date.new(2007,3,14) > => # > irb(main):004:0> age = today.year - bd.year > => 41 > > Consider when today.month < bd.month - you'll have an off-by-one error.