Skip to Main Content
Learn to Program, 2nd Edition
book

Learn to Program, 2nd Edition

by Chris Pine
March 2009
Intermediate to advanced content levelIntermediate to advanced
194 pages
4h
English
Pragmatic Bookshelf
Content preview from Learn to Program, 2nd Edition

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn to Program, 3rd Edition

Learn to Program, 3rd Edition

Chris Pine
The Book of Ruby

The Book of Ruby

Huw Collingbourne

Publisher Resources

ISBN: 9781680500172Errata