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 = self.precase.report.dod
@age ||= begin
a = read_attribute(:age)
dateofbirth = read_attribute(:dob)
if not dateofbirth.nil?
if not dod.nil?
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
dob = DateTime.new(1969,4,24,10,30,0)
dod = DateTime.new(2068,1,31,11,30,0)
age = dod.unit - dob.unit
@age = age.to 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
age.to(another_age) #=> 98.8 year
As you can also see, if you have a string that defines your target
unit, you just need to do...