Exercises from Chapter 12
One Billion Seconds!
Well, I don’t know your brithday, so I don’t know how you’d do it, but here’s how I would do it:
# I don't know what second I was born. |
puts(Time.gm(1976, 8, 3, 13, 31) + 10**9) |
# And yes, I had a party. It was awesome |
# (at least the parts I remember). |
2008-04-11 15:17:40 UTC |
Happy Birthday!
How you could do it:
puts 'What year were you born?' |
b_year = gets.chomp.to_i |
puts 'What month were you born? (1-12)' |
b_month = gets.chomp.to_i |
puts 'What day of the month were you born?' |
b_day = gets.chomp.to_i |
b = Time.local(b_year, b_month, b_day) |
t = Time.new |
age = 1 |
while Time.local(b_year + age, b_month, b_day) <= t |
puts 'SPANK!' |
age = age + 1 |
end |
<= | What year were you born? |
=> | 2009 |
<= | What month were ... |
Get Learn to Program, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.