Another crack at age calculations


#1

I cobbled this together based on a few previous posts and some
tinkering,
nothing fancy (my first bit of Ruby infact) but it seams to work for my
purposes.

My questions is actually why can I call <%= h(child.age(child.dob)) %>
in a view
but not <%= h(child.age_year_month(child.dob)) %>? Both work fine in the
console.

Thanks for any insight,

Hugh

child.rb
class Child < Person

end

person.rb
class Person < ActiveRecord::Base

def age(dob)
unless dob.nil?
years = Date.today.year - dob.year
months = years * 12
months = months + (Date.today.month - dob.month)
years = (months / 12).floor
months = months - (years *12)

return an aray [year,month]

  return years, months
end
nil

end

def age_year_month(dob)
age_my = age(dob)
if age_my[0] == 0
month_year = age_my[1].to_s + " months"
end
if age_my[0] >= 1
month_year = age_my[0].to_s + " years " + age_my[1].to_s + "
months"
end
if age_my[1] == 0
month_year = age_my[0].to_s + " years"
end
return month_year
end

def age_year(dob)
age_years = age(dob)
if age_years[0] == 0
year = age_years[1].to_s + " months"
end
if age_years[0] >= 1
year = age_years[0].to_s + " years"
end
return year
end

end


This message was sent using IMP, the Internet Messaging Program.