Last active
September 25, 2017 18:15
-
-
Save therod/b959474e5a0066192f9dccd6d4666ebd to your computer and use it in GitHub Desktop.
This file contains hidden or 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 '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