#Set @profile_toggle 'on' to turn profiling on
@profile_toggle = 'off'
def profile block_description, &block
if @profile_toggle == 'on'
start_time = Time.now
block.call
duration = Time.now - start_time
puts block_description+' : '+duration.to_s+' seconds'
else
block.call
end
end
# 25000 doublings block
profile '25000 doublings' do
number = 1
25000.times do
number = number + number
end
puts number.to_s.length.to_s+' digits'
end
# count to a million
profile 'count to a million' do
number = 0
1000000.times do
number = number + 1
end
end
2. Grandfather Clock
def clock some_proc
#convert hours to 12 hour format
current_hour = Time.new.hour
if current_hour == 0
current_hour = current_hour + 12
elsif current_hour > 12
current_hour = current_hour - 12
end
#call the proc the number of hours passed
current_hour.to_i.times do
some_proc.call
end
end
# Dong proc
dong_proc = Proc.new do
puts 'DONG!'
end
clock dong_proc
3. Program logger.
def log block_description, &block
puts 'Beginning "'+block_description+'" . . .'
value_returned = block.call
puts '. . . "'+block_description+'" finished, returning:'
puts value_returned
end
log 'outer block' do
log 'some little block' do
5
end
log 'yet another block' do
'I like Thai food!'
end
false
end
4.Better logger.
$nesting_depth = 0
$space = ' '
def log block_description, &block
puts $space*$nesting_depth + 'Beginning "'+block_description+'" ...'
$nesting_depth = $nesting_depth + 1
value_returned = block.call
$nesting_depth = $nesting_depth - 1
puts $space*$nesting_depth + '... "'+block_description+'" finished, returning:'
puts $space*$nesting_depth + value_returned.to_s
end
log 'outer block' do
log 'some little block' do
log 'teeny-tiny block' do
'lots of love'
end
42
end
log 'yet another block' do
'I love Indian food!'
end
true
end