Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Variable value "dump"

Reply
Thread Tools

Variable value "dump"

 
 
Brian Takita
Guest
Posts: n/a
 
      08-26-2005
Hello,

Is there a way to loop through and display all of the local variable
symbols and values in a function?

I would like to use this to log exceptions.

Thank you,
Brian Takita

 
Reply With Quote
 
 
 
 
Brian Takita
Guest
Posts: n/a
 
      08-26-2005
It looks like there is a Kernal.local_variable method that returns an
Array of the local variable names.

So I can print out the local variables with their values this way. Is
there a way to do this without using eval?

a = 1

local_variables.each do |vn|
val = ''
eval("val=#{vn}")
print "#{vn}=#{val}"
end

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-27-2005
Brian Takita <(E-Mail Removed)> wrote:
> It looks like there is a Kernal.local_variable method that returns an
> Array of the local variable names.
>
> So I can print out the local variables with their values this way. Is
> there a way to do this without using eval?


I don't think so.

> a = 1
>
> local_variables.each do |vn|
> val = ''
> eval("val=#{vn}")
> print "#{vn}=#{val}"
> end


Here are some variants

local_variables.each do |vn|
print "#{vn}=#{eval vn}\n"
end

var_dump = local_variables.inject "" do |dump,vn|
dump << vn << "=" << eval(vn).inspect << "\n"
end

Alternatively you can put a binding into the exception that can be used to
retrieve variable values when needed. Semantics are a bit differnt though
(i.e. values might change between the time when the exception is thrown and
the evaluation). Example:

class MyError < Exception
def initialize(msg, bnd)
super(msg)
@bnd = bnd
end

def dump_var
eval("local_variables", @bnd).inject "" do |dump,vn|
dump << vn << "=" << eval(vn, @bnd).inspect << "\n"
end
end
end


def foo(args={})
bar = "hello"
raise MyError.new "something wrong", binding
end

begin
foo( => :u, :name => "nonsense", :numbers => [1,5,67])
rescue MyError => e
puts e, "variables:", e.dump_var
end

Prints

something wrong
variables:
args={=>:u, :numbers=>[1, 5, 67], :name=>"nonsense"}
bar="hello"


Kind regards

robert

 
Reply With Quote
 
Dave Burt
Guest
Posts: n/a
 
      08-27-2005
Brian Takita wrote...
> Is there a way to do this without using eval?
> ...


No, but I feel it should be less than 5 lines:

local_variables.each {|name| puts name + "=" + eval(name).inspect }

Cheers,
Dave


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Evaluate an string variable's value to a variable Mir Nazim Python 2 12-21-2008 07:05 AM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Passing a variable value from Javascript to asp.net server variable shil ASP .Net 2 01-02-2007 09:33 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
create a variable name with the value of another variable Brian C++ 3 04-19-2004 04:35 PM



Advertisments