[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Printing variable name and value

Adam Bender

11/26/2007 3:55:00 AM

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

3 Answers

yermej

11/26/2007 4:40:00 AM

0

On Nov 25, 9:54 pm, Adam Bender <aben...@gmail.com> 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

Barb May

11/3/2013 4:33:00 PM

0

Irish Mike wrote:
> the Obamacare
> roll out has proven to be one of the biggest, and most embarrassing,
> cluster fucks in American history.

Behold the insanity....
--
Barb


Ubiquitous

11/4/2013 4:14:00 PM

0

Someone's sosckpuppet wrote:
>Irish Mike wrote:

>> the Obamacare roll out has proven to be one of the biggest, and most
>> embarrassing, cluster fucks in American history.
>
>Behold the insanity....

Ad hominem noted. Get back to us when you have a real argument to make.

--
"Admittedly, conservatives give as good as they get. The difference
between us and [leftists] is that we can argue as well as inveigh.
They can only hurl invectives." -- Don Feder