Professional Web Applications Themes

print out a variable's name and value? - Ruby

Hello, I want a comfortable debug function to which I can pass a variable, and it writes out the variable's name and value. Can I accomplish this? If I could manipulate the calling stack as bindings, and could get the binding of the caller, then I could pass the variable's name (e.g. as debug(:x)), and make it's name and value printed. If I have to write debug(:x,binding()), then instead I write debug(:x,x). :-)) And I will be sad that I cannot use CPP on ruby source. Cir PS: As you can see, it is rather a theorethical problem... :-)...

  1. #1

    Default print out a variable's name and value?

    Hello,

    I want a comfortable debug function to which I can pass a variable, and
    it writes out the variable's name and value. Can I accomplish this?

    If I could manipulate the calling stack as bindings, and could get the
    binding of the caller, then I could pass the variable's name (e.g. as
    debug(:x)), and make it's name and value printed.

    If I have to write debug(:x,binding()), then instead I write
    debug(:x,x). :-)) And I will be sad that I cannot use CPP on ruby
    source.

    Cir

    PS: As you can see, it is rather a theorethical problem... :-)
    Ferenc Engard Guest

  2. #2

    Default Re: print out a variable's name and value?

    Ferenc Engard [mailto:ferencengard.hu] writes:
    > I want a comfortable debug function to which I can pass a
    > variable, and it writes out the variable's name and value.
    Try this:

    def dbg(&block)
    varname = block.call.to_s
    puts "DBG: #{varname} = #{eval(varname,block)}"
    end

    x = 10
    dbg{:x} # IMPORTANT! Notice curly braces.

    --
    -- Jim Weirich / Compuware
    -- FWP Capture Services
    -- Phone: 859-386-8855

    Weirich, James Guest

  3. #3

    Default Re: print out a variable's name and value?

    "Weirich, James" wrote:
    >
    > Ferenc Engard [mailto:ferencengard.hu] writes:
    > > I want a comfortable debug function to which I can pass a
    > > variable, and it writes out the variable's name and value.
    >
    > Try this:
    >
    > def dbg(&block)
    > varname = block.call.to_s
    > puts "DBG: #{varname} = #{eval(varname,block)}"
    > end
    >
    > x = 10
    > dbg{:x} # IMPORTANT! Notice curly braces.
    WOW! Nice. :-))))

    Cir
    Ferenc Engard Guest

Similar Threads

  1. Creating PDFs from Freehand -- print to PDFvs. export vs. print/scan to PDF?
    By Dan_Kirsch@adobeforums.com in forum Adobe Acrobat Macintosh
    Replies: 0
    Last Post: April 2nd, 11:14 AM
  2. data variable = zero or null, then print "n/a"
    By jsabarese in forum Macromedia ColdFusion
    Replies: 1
    Last Post: March 1st, 12:10 PM
  3. Replies: 1
    Last Post: October 2nd, 04:58 AM
  4. Replies: 0
    Last Post: October 2nd, 04:56 AM
  5. Replies: 1
    Last Post: October 2nd, 04:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139