Age calculation in the model


I'm at my wits end with this problem and hopefully someone can help me.

I have code to calculate age based on date of death - date of birth. I want the age to calculate down to the minute, which it is. However, I have to account for all age units: minutes, hours, days, months, or years. My problem is that I need to have if statements set up for every scenario, but I want to exit the model when the correct age unit has been established. Rails is continuting to evaluate the other if statements, even when I'm checking for nil, and ends up returning 0 in the case of an age unit less than a year. I have tried assembling the if statements in a number of ways, including using a "return" statement when the correct age unit has been calculated. This works, but the model returns an error if there is more than one return statement in existence. I just don't know what else to try!! Any help would be GREATLY appreciated!!

Here's my code:

def age     dod =

    @age ||= begin       a = read_attribute(:age)           dateofbirth = read_attribute(:dob)           if not dateofbirth.nil?             if not dod.nil?

              seconds = dod.to_time - dateofbirth.to_time               minutes = seconds / 60               m = minutes.to_i

            if m.between?(1, 59)                m             end

            hours = minutes / 60             h = hours.to_i

            if not h.nil?               if h.between?(1, 23)                  h               end             end

            days = h / 24             d = days.to_i

            if not d.nil?               if d.between?(1, 30)                d               end             end

            months = days / 30             mths = months.to_i

           if not mths.nil?             if mths.between?(1, 11)                mths             else                years = dod.year - dob.year-(dob.to_time.change(:year => dod.year) > dod ? 1 : 0)             end            end         end      end   end end

Couple of things here...

1. use the 'ruby-units' gem to help with the unit conversions

2. use a 'case'

require 'rubygems' require 'ruby-units' require 'date'

dob =,4,24,10,30,0) dod =,1,31,11,30,0)

age = dod.unit - dob.unit

@age = case   when age < '1 min'.unit : 'sec'   when age < '1 hr'.unit : 'min'   when age < '1 day'.unit : 'hour'   when age < '1 week'.unit : 'day'   when age < '1 year'.unit : 'week'   when age < '10 years'.unit : 'year'   when age < '100 years'.unit : 'decade' else    'century' end

puts "#{'%0.2f' % @age}"

#=> 9.88 decade

Note that ruby-units doesn't define a 'month' unit (because it isn't fixed). But you can define one that is 30 days long if you like. You also want to use the format string in the to_s or you get this...

15900642/15778463 century

Because DateTime likes to make things Rational.

If you want the output to match the units of another compatible unit just pass it in the 'to' call

age = '9.88 decade'.unit another_age = '77 year'.unit #=> 98.8 year

As you can also see, if you have a string that defines your target unit, you just need to do...


Almost forgot... ruby-units also supports ranges, so this works too

@age = case age   when '0 sec'.unit ...'1 min'.unit : 'sec'   when '1 min'.unit ... '1 hr'.unit : 'min'   when '1 hr'.unit ... '1 day'.unit : 'hour'   when '1 day'.unit ... '1 week'.unit : 'day'   when '1 week'.unit ... '1 year'.unit : 'week'   when '1 year'.unit ... '10 years'.unit : 'year'   when '10 years'.unit ... '100 years'.unit : 'decade' else   'century' end