P
Phlip
Rubies:
Consider this excellent trace_{} method, based on code posted here by one
Carlos, many winters ago:
def trace_(&b)
file, line_nr, funkshun = caller()[0].split(':')
# sorry, folks - split's the only stringey function I know ;-)
funkshun = funkshun.split('in `')[1]
funkshun = funkshun.split("'")[0]
line_nr=line_nr.to_i
linecnt=1
expr=nil
File.open(file).each_line do |line|
if linecnt==line_nr
if line =~ /(^|\W)trace_\s*\{\s*(.*?)\s*\}/
expr = $2
end
break
end
linecnt+=1
end
if expr
puts "(#{line_nr})#{funkshun} #{expr}: #{eval(expr,
b).inspect}"
end #{file}
end
All it does is this:
require 'trace'
def foo()
complicatedThing = "simple thing"
trace_{complicatedThing}
end
foo()
....
(6)foo complicatedThing: "simple thing"
It prints out the file name (if any), function name, and line number of
'complicatedThing', and it reflects 'complicatedThing' itself
(A side question: This trace_ is nice, but does the latest Ruby have any
better ways to do all this?)
Here's the question. That code parses the calling source file, and uses
/(^|\W)trace_\s*\{\s*(.*?)\s*\}/ stuff to find itself.
Is there a way to extract the column number from caller(), or its relatives,
so we can accurately grab the {} and not worry about its relative location
in a file?
Long post; short question.
Consider this excellent trace_{} method, based on code posted here by one
Carlos, many winters ago:
def trace_(&b)
file, line_nr, funkshun = caller()[0].split(':')
# sorry, folks - split's the only stringey function I know ;-)
funkshun = funkshun.split('in `')[1]
funkshun = funkshun.split("'")[0]
line_nr=line_nr.to_i
linecnt=1
expr=nil
File.open(file).each_line do |line|
if linecnt==line_nr
if line =~ /(^|\W)trace_\s*\{\s*(.*?)\s*\}/
expr = $2
end
break
end
linecnt+=1
end
if expr
puts "(#{line_nr})#{funkshun} #{expr}: #{eval(expr,
b).inspect}"
end #{file}
end
All it does is this:
require 'trace'
def foo()
complicatedThing = "simple thing"
trace_{complicatedThing}
end
foo()
....
(6)foo complicatedThing: "simple thing"
It prints out the file name (if any), function name, and line number of
'complicatedThing', and it reflects 'complicatedThing' itself
(A side question: This trace_ is nice, but does the latest Ruby have any
better ways to do all this?)
Here's the question. That code parses the calling source file, and uses
/(^|\W)trace_\s*\{\s*(.*?)\s*\}/ stuff to find itself.
Is there a way to extract the column number from caller(), or its relatives,
so we can accurately grab the {} and not worry about its relative location
in a file?
Long post; short question.