Created
November 2, 2014 05:48
-
-
Save zosiu/e840087e8807d494d1f5 to your computer and use it in GitHub Desktop.
age calculation in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'time' | |
class Human | |
attr_reader :birth_date | |
def initialize(birth_date) | |
@birth_date = birth_date | |
end | |
def age | |
now = Time.now.utc.to_date | |
correction = now.strftime('%m%d') < birth_date.strftime('%m%d') ? 1 : 0 | |
now.year - birth_date.year - correction | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment