Exercises from Chapter 2

Hours in a Year

How you could do it:

puts 24*365
8760

How I would do it:

# depends on if it's a leap year
puts 24*365
puts ​"(or ​#{24*366}​ on occasion)"
8760
(or 8784 on occasion)

Minutes in a Decade

How you could do it:

puts 60*24*(365*10 + 2)
5258880

How I would do it:

# depends on how many leap years in that decade
puts ​"​#{60*24*(365*10 + 2)}​ or ​#{60*24*(365*10 + 3)}​"
5258880 or 5260320

Your Age in Seconds

How you could do it:

puts 60*60*24*365*36
1135296000

How I would do it:

puts(Time.new - Time.gm(1976, 8, 3, 13, 31))
1204369530.403

Our Dear Author’s Age

How you could do it:

puts 1160000000/(60*60*24*365)
36

And that’s pretty much how I would do it, too.

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.