-
Notifications
You must be signed in to change notification settings - Fork 0
/
chap_10_c.rb
50 lines (39 loc) · 1.44 KB
/
chap_10_c.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
=begin
Better Logger. The output from that last logger was kind of hard to read, and it
would just get worse the more you used it. It would be so much easier to read if
it indented the lines in the inner blocks. To do this, you'll need to keep track
of how deeply nested you are every time the logger wants to write something. To
do this, use a global variable, a variable you can see from anywhere in your
code. To make a global variable, just precede your variable name with $, like
these: $global, $nestingDepth, and $bigTopPeeWee. In the end, your logger
should output code like this:
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
=end
$nestingDepth = 0
$indent = " "
def log description, &block
puts $indent * $nestingDepth + 'Beginning "'+description+'"...'
$nestingDepth = $nestingDepth + 1
return_val = block.call
$nestingDepth = $nestingDepth - 1
puts $indent * $nestingDepth + '..."'+description+'" finished, returning: ' + return_val.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