class Date

Public Instance Methods

age() click to toggle source

Return age of today

# File lib/date_age.rb, line 22
def age
  age_at(Date.today)
end
age_at(date) click to toggle source

Return age at specified `date`

# File lib/date_age.rb, line 6
def age_at(date)
  unless date.respond_to?(:year) && date.respond_to?(:month) && date.respond_to?(:day)
    raise ArgumentError, "age_at(date): date should respond to :year, :month and :day"
  end
  birthday = self
  today    = date
  age = today.year - birthday.year
  if today.month < birthday.month
    age -= 1
  elsif birthday.month == today.month
    age -= 1 if today.day < birthday.day
  end
  age
end