Created
May 18, 2012 17:27
-
-
Save localshred/2726568 to your computer and use it in GitHub Desktop.
Birthdays date calculator to show num days alive, leap years lived through, average age of a group of people (e.g. your fam). I dunno, fun to write at least.
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 'date' | |
today = Date.today | |
peeps = [ | |
[:bj, 1983, 1, 18], | |
[:angelee, 1983, 12, 27], | |
[:bella, 2004, 11, 17], | |
[:anders, 2008, 6, 30], | |
[:india, 2012, 2, 23] | |
] | |
t = 0 | |
peeps.each do |args| | |
name = args.shift | |
bday = Date.new(*args) | |
leap_years = bday.year.upto(today.year).inject([]) do |sum, y| | |
if (y.modulo(4) == 0 && y.modulo(100) != 0) || (y.modulo(4) == 0 && y.modulo(100) == 0 && y.modulo(1000) == 0) | |
sum << y | |
else | |
sum | |
end | |
end | |
num_days = (today - bday).to_i | |
years = (num_days.to_f / 365.0).to_i | |
remainder_days = num_days - (years * 365) - leap_years.size | |
puts '%s is %d days old (%d years, %d days) (leap years lived %s)' % [name, num_days, years, remainder_days, leap_years.join(',')] | |
t += num_days | |
end | |
total_yrs = t.to_f / 365.0 | |
average_age = total_yrs / peeps.size.to_f | |
puts | |
puts 'Collectively, we are:' | |
puts "\t%d days old" % t | |
puts "\t%0.2f years old" % total_yrs | |
puts "\tAverage age %0.2f years old" % average_age |
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
bj is 10713 days old (29 years, 120 days) (leap years lived 1984,1988,1992,1996,2000,2004,2008,2012) | |
angelee is 10370 days old (28 years, 142 days) (leap years lived 1984,1988,1992,1996,2000,2004,2008,2012) | |
bella is 2739 days old (7 years, 181 days) (leap years lived 2004,2008,2012) | |
anders is 1418 days old (3 years, 321 days) (leap years lived 2008,2012) | |
india is 85 days old (0 years, 84 days) (leap years lived 2012) | |
Collectively, we are: | |
25325 days old | |
69.38 years old | |
Average age 13.88 years old |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment