Skip to content

Instantly share code, notes, and snippets.

@zosiu
Created November 2, 2014 05:48
Show Gist options
  • Save zosiu/e840087e8807d494d1f5 to your computer and use it in GitHub Desktop.
Save zosiu/e840087e8807d494d1f5 to your computer and use it in GitHub Desktop.
age calculation in ruby
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