Forum: Ruby on Rails Another crack at age calculations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Hugh Kelsey (Guest)
on 2006-01-25 23:54
(Received via mailing list)
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.
This topic is locked and can not be replied to.