Skip to content

Instantly share code, notes, and snippets.

@alindeman
Created October 23, 2012 20:49
Show Gist options
  • Save alindeman/3941456 to your computer and use it in GitHub Desktop.
Save alindeman/3941456 to your computer and use it in GitHub Desktop.
class Age
def self.for_date_of_birth(dob, today = Date.today)
# http://stackoverflow.com/a/11942/445124
diff = today.strftime("%Y%m%d").to_i - dob.strftime("%Y%m%d").to_i
diff / 10000
end
end
require_relative "../../app/models/age"
describe Age do
it "calculates a person's age given their birth date and the current date" do
expect(Age.for_date_of_birth(Date.new(1980, 7, 3), Date.new(2008, 8, 14))).to eql 28
end
it "calculates a person's age who was born on a leap year February 29" do
expect(Age.for_date_of_birth(Date.new(2000, 2, 29), Date.new(2009, 2, 28))).to eql 8
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment