Imobach Gonz?lez Sosa wrote:
> Hi all,
>
> We're interested in doing some backtracing in our code, but not just
> the "filename:line in method" thing, we'd like to retrieve the status
> of each object referenced.
>
> For example, we have three objects A, B and C. A calls a B's method
> and B calls an C's method. That's ok. But the C methods raises and
> exception. At A, we could get a backtrace containing files, lines and
> methods called. But, what about some extra info as, for example, B
> status?
>
> We've implemented such a thing using begin-end-rescue in all those
> called methods, so in the rescue they do some extra work to register
> the status. Ok?
>
> But write a lot of methods with begin-end-rescue could be painful, at
> least for me.

Yes, this *is* painful.

> Does anybody understand me? ;-) Any idea about it?
>
> Sorry if the question sounds stupid :-P

Not at all.  I'd have a look at set_trace_func:
http://www.ruby-doc.org/core/classes/Kernel.html#M001743

You have bindings available that contain the current value of "self".
Here's a very small example:

09:24:36 [4.5_Versions]: ruby <<EOF
> def foo(s)
>   puts s.length
> end
> set_trace_func proc { |event, file, line, id, binding, classname|
>   printf "%20s %s\n", event, eval("self",binding).inspect
> }
> foo "hello"
> EOF
                line main
                call main
                line main
              c-call "hello"
            c-return "hello"
              c-call main
              c-call 5
            c-return 5
              c-call 5
            c-return 5
              c-call #<IO:0x100ede40>
5            c-return #<IO:0x100ede40>
              c-call #<IO:0x100ede40>

            c-return #<IO:0x100ede40>
            c-return main
              return main
09:24:53 [4.5_Versions]:

You can instrument all call and return events to store self on a stack and
retrieve those values in case of an exception.

Kind regards

    robert