Skip to content

Instantly share code, notes, and snippets.

@Lockyy
Last active June 6, 2023 16:23
Show Gist options
  • Save Lockyy/f927cc0f98ae84fc8b4341f6029c8d71 to your computer and use it in GitHub Desktop.
Save Lockyy/f927cc0f98ae84fc8b4341f6029c8d71 to your computer and use it in GitHub Desktop.
Monkey patch onto Date/Time/DateTime objects to allow them to be converted to an age, accommodating for leap years
def to_age
today = Time.current
year_diff = today.year - year
today_is_past_birthday = today.month > month || (today.month == month && today.day >= day)
today_is_past_birthday ? year_diff : year_diff - 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment