# Trace local variable

**URL:** <https://www.ruby-forum.com/t/trace-local-variable/183990>\
**Category:** Ruby\
**Created:** [February 23, 2010, 4:24pm UTC](https://www.ruby-forum.com/t/trace-local-variable/183990 "2010-02-23T16:24:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![onionwushu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5daacb/32.png) [@onionwushu](https://www.ruby-forum.com/u/onionwushu)\
**Post date:** [February 23, 2010, 4:24pm UTC](https://www.ruby-forum.com/t/trace-local-variable/183990/1 "2010-02-23T16:24:28Z")

</div>

Hi !

I’ve searched a lot to get an answer but I can’t figured it out. I wish  
to trace local variables in a method. Here is an example:

def test  
a = 1  
b = 2  
end

Returns:  
Variable a gets the value 1 in test  
Variable b gets the value 2 in test

So, I’d like this output without adding some extra stuff in my method  
test. I need a callback or thing like that but I can’t find it.

I’m aware of the built-in method trace\_var, but unfortunately, it’s all  
about global variables. So, I successed to trace attribute calls,  
because it is possible to define methods like this:

def attribute=(value)  
…  
end

I also successed to trace method calls.

I’ve seen function like set\_trace\_func but it’s not really convenient to  
deal with my particular problem. And it isn’t possible to redefine the  
assignment operator = on its own.

Any ideas to trace local variables ?  
Thanks !

---

<div class="post-metadata">

**Author:** ![onionwushu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5daacb/32.png) [@onionwushu](https://www.ruby-forum.com/u/onionwushu)\
**Post date:** [February 24, 2010, 9:21am UTC](https://www.ruby-forum.com/t/trace-local-variable/183990/2 "2010-02-24T09:21:48Z")

</div>

2010/2/23 Onionwushu O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Variable b gets the value 2 in test  
> end
> 
> I also successed to trace method calls.
> 
> I’ve seen function like set\_trace\_func but it’s not really convenient to  
> deal with my particular problem. And it isn’t possible to redefine the  
> assignment operator = on its own.
> 
> Any ideas to trace local variables ?

I don’t think there is an easy way with MRI and standard tools. You  
could debug the code of course. Other than that, these are things  
that I can imagine to work:

- patch interpreter to generate output for variable access
- use set\_trace\_func along with a parsed version of the source (e.g.  
ParseTree)
- patch interpreter even more to allow for a similar mechanism like  
set\_trace\_func (aka “set\_trace\_local”).

Btw, why do you need that feature? If your methods are so complex  
that you need tracing of individual variables maybe your methods are  
too long.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![onionwushu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5daacb/32.png) [@onionwushu](https://www.ruby-forum.com/u/onionwushu)\
**Post date:** [February 25, 2010, 9:46am UTC](https://www.ruby-forum.com/t/trace-local-variable/183990/3 "2010-02-25T09:46:19Z")

</div>

Thank you for your proposals. I’ll prefer to avoid patching the  
interpreter for now 😛  
I need this feature because I’d like to know each time a local variable  
is updated. So, I found a way using set\_trace\_func and the binding  
attribute, here it is:

set\_trace\_func lambda { |event, file, line, id, binding, classname|  
if event == “line” or event == “return”  
local = eval(“local\_variables”, binding)  
local.each do |l|  
puts “#{l} : #{eval(l, binding)}”  
end  
puts “======”  
end  
}

I suppose this is not really nice about performance, but it’s working 😛

---

<div class="post-metadata">

**Author:** ![onionwushu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5daacb/32.png) [@onionwushu](https://www.ruby-forum.com/u/onionwushu)\
**Post date:** [February 26, 2010, 12:43am UTC](https://www.ruby-forum.com/t/trace-local-variable/183990/4 "2010-02-26T00:43:50Z")

</div>

But that looks ugly ☹

I can not believe ruby could make it so difficult just to be able to  
trace local variables? It’s a lot easier with global variables, but I  
usually don’t use many global variables …
