# Variable Dump

**URL:** <https://www.ruby-forum.com/t/variable-dump/77959>\
**Category:** Ruby\
**Created:** [December 27, 2005, 9:45pm UTC](https://www.ruby-forum.com/t/variable-dump/77959 "2005-12-27T21:45:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Smorey\_Jr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@Dan\_Smorey\_Jr](https://www.ruby-forum.com/u/Dan_Smorey_Jr)\
**Post date:** [December 27, 2005, 9:45pm UTC](https://www.ruby-forum.com/t/variable-dump/77959/1 "2005-12-27T21:45:52Z")

</div>

I’d like to know how to grab a dump of all variable associated with a  
script. Here is a test script I am using…

#!/usr/bin/env ruby

class Binding  
def info\_locals()  
vars = eval(“local\_variables”, self)  
vars.each { |v| puts %(#{v} --\> #{eval(v, self).inspect}) }  
end  
end

def foo\_def  
inside\_foo\_def = “hello there”  
binding.info\_locals  
end

foo = ‘hello’  
bar = “goodbye”  
array = [‘one’, ‘two’]

foo\_def()

I get this output…

inside\_foo\_def --\> “hello there”

That’s the problem. I do not get all variables, just the variable in  
the def foo\_def scope. I’d like to be able to grab foo, bar, and  
array also, much like this…

inside\_foo\_def --\> “hello there”  
foo --\> “hello”  
bar --\> “bar”  
array --\> [‘one’, ‘two’]

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Dan\_Smorey\_Jr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@Dan\_Smorey\_Jr](https://www.ruby-forum.com/u/Dan_Smorey_Jr)\
**Post date:** [December 27, 2005, 10:40pm UTC](https://www.ruby-forum.com/t/variable-dump/77959/2 "2005-12-27T22:40:10Z")

</div>

Dan Smorey Jr. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> foo\_def()  
> foo → “hello”  
> bar → “bar”  
> array → [‘one’, ‘two’]
> 
> Thanks in advance.

You would have to walk up the call stack to get locals of surrounding  
environments. Kernel.set\_trace\_func may help here.

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![Dan\_Smorey\_Jr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@Dan\_Smorey\_Jr](https://www.ruby-forum.com/u/Dan_Smorey_Jr)\
**Post date:** [December 28, 2005, 12:58am UTC](https://www.ruby-forum.com/t/variable-dump/77959/3 "2005-12-28T00:58:57Z")

</div>

this clumsy script gets you part of the way there.  
maybe somebody else knows a recursive solution.

#!/usr/bin/env ruby

$top\_level = binding

class Binding  
def info\_vars(b = self)  
vars = eval(“local\_variables”, b)  
vars.each { |v| puts %(#{v} --\> #{eval(v, b).inspect}) }  
end  
end

def foo\_def  
inside\_foo\_def = “hello there”

```
 binding.info_vars
 puts "\n# top level:"
 binding.info_vars $top_level

```

end

foo = ‘hello’  
bar = “goodbye”  
array = [‘one’, ‘two’]  
foo\_def()
