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
hour = 12
end
hour.times ​do
block.call
end
end
grandfather_clock ​do
puts ​'DONG!'
end
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!

How I would do it:

def​ grandfather_clock &block
hour = (Time.new.hour + 11)%12 + 1
hour.times(&block)
end
grandfather_clock { puts ​'DONG!'​ }
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!
DONG!

Program Logger

How you could do it:

def​ log desc, &block
puts ​'Beginning "'​ + desc + ​'"...'
result = block.call
puts ​'..."'​ + desc + ​'" finished, returning: '​ + result.to_s
end
log ​'outer block'​ ​do
log ​'some little block'​ ​do
1**1 + 2**2
end
log ​'yet another block'​ ​do
'!doof iahT ekil I'​.reverse
end
'0'​ == 0
end
Beginning "outer block"...
Beginning "some little block"...
..."some little block" finished, returning: 5
Beginning "yet another block"...
..."yet another block" finished, returning: I like Thai food!
..."outer block" finished, returning: false

How I would do it:

def​ log desc, &block
puts ​"Beginning ​#{desc.inspect}​..."
result = block[]
puts ​"...​#{desc.inspect}​ finished, returning: ​#{result}​"
end
log ​'outer block'​ ​do
log ​'some little block'​ ​do
1**1 + 2**2
end
log ​'yet another block'​ ​do
'!doof iahT ekil I'​.reverse
end
'0'​ == 0
end
Beginning "outer block"...
Beginning "some little block"...
..."some little block" finished, returning: 5
Beginning "yet another block"...
..."yet another block" finished, returning: I like Thai food!
..."outer block" finished, returning: false

Better Program Logger

How you could do it:

$logger_depth = 0
def​ log desc, &block
prefix = ​' '​*$logger_depth
puts prefix + ​'Beginning "'​ + desc + ​'"...'
$logger_depth = $logger_depth + 1
result = block.call
$logger_depth = $logger_depth - 1
puts prefix + ​'..."'​ + desc + ​'" finished, returning: '​ + result.to_s
end
log ​'outer block'​ ​do
log ​'some little block'​ ​do
log ​'teeny-tiny block'​ ​do
'lOtS oF lOVe'​.downcase
end
7 * 3 * 2
end
log ​'yet another block'​ ​do
'!doof naidnI evol I'​.reverse
end
'0'​ == ​"0"
end
Beginning "outer block"...
Beginning "some little block"...
Beginning "teeny-tiny block"...
..."teeny-tiny block" finished, returning: lots of love
..."some little block" finished, returning: 42
Beginning "yet another block"...
..."yet another block" finished, returning: I love Indian food!
..."outer block" finished, returning: true

How I would do it:

$logger_depth = 0
def​ log desc, &block
prefix = ​' '​*$logger_depth
puts prefix+​"Beginning ​#{desc.inspect}​..."
$logger_depth += 1
result = block[]
$logger_depth -= 1
puts prefix+​"...​#{desc.inspect}​ finished, returning: ​#{result}​"
end
log ​'outer block'​ ​do
log ​'some little block'​ ​do
log ​'teeny-tiny block'​ ​do
'lOtS oF lOVe'​.downcase
end
7 * 3 * 2
end
log ​'yet another block'​ ​do
'!doof naidnI evol I'​.reverse
end
'0'​ == ​"0"
end
Beginning "outer block"...
Beginning "some little block"...
Beginning "teeny-tiny block"...
..."teeny-tiny block" finished, returning: lots of love
..."some little block" finished, returning: 42
Beginning "yet another block"...
..."yet another block" finished, returning: I love Indian food!
..."outer block" finished, returning: true
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.195.236