-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.rb
41 lines (39 loc) · 866 Bytes
/
debug.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
# 19/08/10
module Debug
# Log to a file either a) the value of some variables at the point of
# invocation or b) a message.
#
# The variables are passed inside a block as an array of symbols.
def Debug.debug(opts={}, &blk)
msg = []
if opts.include? :msg
msg << "#{opts[:msg]}"
end
if block_given?
(yield.map &:to_s).each do |var|
val = eval var, blk.binding
msg << "#{var} = #{val}"
end
end
unless msg.empty?
msg.unshift caller.last.to_s
log = opts[:log] || "debug.log"
File.open log, "a" do |log|
log.write msg.join("\n")
log.write "\n\n"
end
end
end
end
if __FILE__ == $0
x = 1
y = 2
Debug.debug { [:x, :y] }
z = 3
Debug.debug { [:x, :y, :z] }
Debug.debug msg: "Test"
Debug.debug
Debug.debug msg: "Test 2" do
[:x, :y]
end
end