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 14

Even Better Profiling

How you could do it:

def​ profile block_description, &block
# To turn profiling on/off, set this
# to true/false.
profiling_on = false
if​ profiling_on
start_time = Time.new
block.call
duration = Time.new - start_time
puts ​"​#{block_description}​: ​#{duration}​ seconds"
else
block.call
end
end

How I would do it:

$OPT_PROFILING_ON = false
def​ profile block_description, &block
if​ $OPT_PROFILING_ON
start_time = Time.new
block[]
duration = Time.new - start_time
puts ​"​#{block_description}​: ​#{duration}​ seconds"
else
block[]
end
end

Grandfather Clock

How you could do it:

def​ grandfather_clock &block
hour = Time.new.hour
if​ hour >= 13
hour = hour - 12
end
if​ hour == 0
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