Just when I thought that I had perfectly understood the IF statement in
Ruby, I had this humbling revelation:
(line numbers for reference only)
···
#----------------------------------------------------------------
01: set_trace_func proc{|event, file, line, objId, binding, klass|
02: p [file,line] if(event ==
‘line’) }
03:
04: a = 0 #<---- Remove this 0
05: if
06: true
07: puts 'here’
08: 10
09: else
10: puts 'there’
11: 20
12: end
13:
14: p a
15:
16: set_trace_func(nil) #-----------------------------------------------------------------
This produces:
["C:/atest/tst_set_trace.rb", 4]
["C:/atest/tst_set_trace.rb", 6] <<< This is ok...
["C:/atest/tst_set_trace.rb", 6] <<< But why this ?
["C:/atest/tst_set_trace.rb", 7]
here
["C:/atest/tst_set_trace.rb", 8]
["C:/atest/tst_set_trace.rb", 14]
0
["C:/atest/tst_set_trace.rb", 16]
Notice the line 6 traced twice ??
And now remove the 0 (just the constant, not the 'a = ’ ) on line 4
to get:
A feature. This is added to accomplish better debugger tracing.
The call timing may be changed in the future.
matz.
Events are: c-call, c-return, call , class, end,
line (execute code on a new line), raise and return.
</>
puts “ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]”
set_trace_func proc {| ev, fi, li, oi, bi, cl |
printf(“(%8s) %s:%-2d (%10s) %s %8s\n”,
ev, fi[-11,11], li, oi.inspect, bi.to_s, cl) if ev == ‘line’
}
##- line 7
a =
1
a = 2 if true
if true then a = 3 end
a -= 1 while a > 0
Perhaps if calls other than the first were ‘line+’, the debugger could
accept ‘line’ and ‘line+’ while others could easily filter out the
extra detail.
We’re not all ‘node-dumpers’
Very many thanks, Matz,
daz
“Not only that: He’s handed out the fkn source code as well!”
I took this to mean that the frequency and/or sequence of
calls is “undefined” at present. Don’t write code in a
set_trace_func proc that depends on ‘line’ events being the
same in the future as they are now.
If this gives you a problem, you can save the line
number so that your proc is called only once per line.
Note that some single-line iterations are filtered out (lost)
even though multiple genuine new-line calls were received.
What is “call timing” ? Time when a particular call was made ?
#----------------------------------------------------------------
prev_line = nil
set_trace_func proc {| event, fi, line, oi, bi, cl |
case event
when ‘line’
if (( true)) and prev_line == line # ← change to ((!true))
print ‘[+ ]’
else
printf(“[%-2d]”, line)
end
else
print ‘.’ # other events
end
prev_line = line
}
line :15
def roo
3.times do
0
end
end
a =
1
a = 2 if true
if true then a = 3 end
a = 4; a -= 1 while a > 0
2.times do
z = 5
end
2.times do
z = 6
+7
end
roo #----------------------------------------------------------------
Change (( true)) to (( !true)) on line 5 to remove the filter
[+ ] shows a repeat of the prior line [nn]
all other events show as dots
Results:
Two long lines from two runs split and grouped (IfYouKnowWhatIMean)
Keep track of line number in the executing source file, for debugging
and error reporting.
Since I am not as conversant with Ruby source as you all are, I am still not
quite getting it. But looking at his reply to this question, I am sure Matz
is
planning to use it in future (for “call timing”).