Le 14 mars 15:47, Deniz Dogan a ˝─rit :

> 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.

Depends on what you want exactly.  If you want to compute the "usual"
age (i.e. I'll say I'm 31 years old, not 31.7426420260096), you might
try this :

>> d1 = Date.today
=> #<Date: 4908347/2,0,2299161>
>> d2 = Date.new(1975,6,16)
=> #<Date: 4885159/2,0,2299161>
>> (d1.year - d2.year) - (d1.yday >= d2.yday ? 0 : 1)
=> 31
>> d1 = Date.new(2007, 06, 16)
=> #<Date: 4908535/2,0,2299161>
>> (d1.year - d2.year) - (d1.yday >= d2.yday ? 0 : 1)
=> 32

Fred
-- 
Assignments: telling a variable what it stands for, and/or what value(s)
it should have is coercive and paternalistic: variables should be free
to choose their own names and value-sets from a range of non-sexist,
non-racist options.   (Tanuki in the SDM, on politically-correct coding)