Retrieve column # of calling function

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 :wink:

        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.

···

--
  Phlip
  http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces

Phlip wrote:

require 'trace'

def foo()
    complicatedThing = "simple thing"
    trace_{complicatedThing}
end

You can also do that with Binding.of_caller (see attachment), btw:

def trace(code)
   Binding.of_caller do |context|
     file, line = eval("[__FILE__, __LINE__]", context)
     result = eval(code.to_s, context)
     STDERR.puts "In #{file} at #{line}: #{code} => #{result}"
   end
end

something = 5
trace :something
trace "[self, something]"

Regards,
Florian Gross

binding_of_caller.rb (2.56 KB)

Florian Gross wrote:

Phlip wrote:

> require 'trace'
>
> def foo()
> complicatedThing = "simple thing"
> trace_{complicatedThing}
> end

You can also do that with Binding.of_caller (see attachment), btw:

def trace(code)
   Binding.of_caller do |context|
     file, line = eval("[__FILE__, __LINE__]", context)
     result = eval(code.to_s, context)
     STDERR.puts "In #{file} at #{line}: #{code} => #{result}"
   end
end

something = 5
trace :something
trace "[self, something]"

Thanks; that's good to know. But...

....I'm primarily interested in the _column_number_. Upgrading trace_{} is a
nice second.

···

--
  Phlip
  http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces