Variable value "dump"

Discussion in 'Ruby' started by Brian Takita, Aug 26, 2005.

  1. Brian Takita

    Brian Takita Guest

    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
     
    Brian Takita, Aug 26, 2005
    #1
    1. Advertising

  2. Brian Takita

    Brian Takita Guest

    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
     
    Brian Takita, Aug 26, 2005
    #2
    1. Advertising

  3. Brian Takita <> 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:)x => :u, :name => "nonsense", :numbers => [1,5,67])
    rescue MyError => e
    puts e, "variables:", e.dump_var
    end

    Prints

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


    Kind regards

    robert
     
    Robert Klemme, Aug 27, 2005
    #3
  4. Brian Takita

    Dave Burt Guest

    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
     
    Dave Burt, Aug 27, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. halfdog
    Replies:
    12
    Views:
    12,463
  2. martin f krafft
    Replies:
    1
    Views:
    328
    Marc 'BlackJack' Rintsch
    Mar 17, 2008
  3. Dan Smorey Jr.

    Variable Dump

    Dan Smorey Jr., Dec 27, 2005, in forum: Ruby
    Replies:
    2
    Views:
    104
    Lou Vanek
    Dec 27, 2005
  4. Ralph Shnelvar
    Replies:
    0
    Views:
    151
    Ralph Shnelvar
    Dec 25, 2009
  5. Bill H

    Variable dump

    Bill H, Jul 29, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    227
Loading...

Share This Page