[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Variable Dump

Dan Smorey Jr.

12/27/2005 8:45:00 PM

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.


2 Answers

Robert Klemme

12/27/2005 9:37:00 PM

0

Dan Smorey Jr. <dsmorey@gmail.com> wrote:
> 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.

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

vanekl

12/27/2005 11:58:00 PM

0