Skip to content

Instantly share code, notes, and snippets.

@therod
Last active September 25, 2017 18:15
Show Gist options
  • Save therod/b959474e5a0066192f9dccd6d4666ebd to your computer and use it in GitHub Desktop.
Save therod/b959474e5a0066192f9dccd6d4666ebd to your computer and use it in GitHub Desktop.
require 'date'
require 'active_support/all'
# Calcucaltes the age from a provided birthdate
#
# Returns - Age as Integer
def get_age(birthdate)
today = Date.today
birthdate = Date.parse(birthdate)
years = (today.year - birthdate.year)
birthday_this_year = birthdate + years.years
if birthday_this_year >= today
years - 1
else
years
end
end
print 'Please enter your Birthdate (dd.mm.yyyy) > '
input = gets.chomp
result = get_age(input)
puts "You are #{get_age(input)} years old"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment