Printing variable name and value

Discussion in 'Ruby' started by Adam Bender, Nov 26, 2007.

  1. Adam Bender

    Adam Bender Guest

    Note: parts of this message were removed by the gateway to make it a legal Usenet post.

    Hi, I want a function that will pretty-print the name and value of a
    variable, so I can avoid writing things like:
    puts "x is #{x}"

    Ideally, I would just be able to pass the variable, or at most a label, to a
    function, and have it do this for me. I wrote something that almost works;
    I have to pass the current binding as well. Is there a way to avoid this?
    Is there a better way to do what I've done below? Thanks,

    Adam

    ===============================

    #!/usr/bin/ruby

    def pprint(label, bind)
    typ = eval(label.to_s, bind).class.name
    case typ
    when "Array"
    arr = eval(label.to_s, bind)
    puts label.to_s + " is [" + arr.join(", ") + "]"

    when "Hash"
    hash = eval(label.to_s, bind)
    puts label.to_s + " is {" +
    hash.collect { |k, v| "#{k} => #{v}" }.join(", ") + "}"

    when "Fixnum"
    puts label.to_s + " is " + eval(label.to_s, bind).to_s

    when "String"
    puts label.to_s + " is " + eval(label.to_s, bind)

    else
    puts "unknown type: #{typ}"
    end
    end

    a = 3
    pprint :a, binding

    y = "aasdf"
    pprint :y, binding

    x = [1, 2, 3]
    pprint :x, binding

    z = {'a' => 1, 'b' => 3}
    pprint :z, binding
     
    Adam Bender, Nov 26, 2007
    #1
    1. Advertising

  2. Adam Bender

    yermej Guest

    On Nov 25, 9:54 pm, Adam Bender <> wrote:
    > Note: parts of this message were removed by the gateway to make it a legal Usenet post.
    >
    > Hi, I want a function that will pretty-print the name and value of a
    > variable, so I can avoid writing things like:
    > puts "x is #{x}"
    >
    > Ideally, I would just be able to pass the variable, or at most a label, to a
    > function, and have it do this for me. I wrote something that almost works;
    > I have to pass the current binding as well. Is there a way to avoid this?
    > Is there a better way to do what I've done below? Thanks,
    >
    > Adam
    >
    > ===============================
    >
    > #!/usr/bin/ruby
    >
    > def pprint(label, bind)
    > typ = eval(label.to_s, bind).class.name
    > case typ
    > when "Array"
    > arr = eval(label.to_s, bind)
    > puts label.to_s + " is [" + arr.join(", ") + "]"
    >
    > when "Hash"
    > hash = eval(label.to_s, bind)
    > puts label.to_s + " is {" +
    > hash.collect { |k, v| "#{k} => #{v}" }.join(", ") + "}"
    >
    > when "Fixnum"
    > puts label.to_s + " is " + eval(label.to_s, bind).to_s
    >
    > when "String"
    > puts label.to_s + " is " + eval(label.to_s, bind)
    >
    > else
    > puts "unknown type: #{typ}"
    > end
    > end
    >
    > a = 3
    > pprint :a, binding
    >
    > y = "aasdf"
    > pprint :y, binding
    >
    > x = [1, 2, 3]
    > pprint :x, binding
    >
    > z = {'a' => 1, 'b' => 3}
    > pprint :z, binding


    Check the group archives as this was discussed recently. The example
    you gave could be written as:

    def pprint(label, bind)
    puts "#{label} is #{eval(label.to_s, bind).inspect}"
    end

    Jeremy
     
    yermej, Nov 26, 2007
    #2
    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. Brian
    Replies:
    3
    Views:
    428
    Siemel Naran
    Apr 19, 2004
  2. Replies:
    6
    Views:
    323
  3. Bobby Chamness
    Replies:
    2
    Views:
    2,412
    Joe Smith
    Apr 22, 2007
  4. mfglinux
    Replies:
    11
    Views:
    712
    Roberto Bonvallet
    Sep 12, 2007
  5. David Filmer
    Replies:
    19
    Views:
    250
    Kevin Collins
    May 21, 2004
Loading...

Share This Page