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 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.

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