How to get Ruby objects printed out in their 'natural' form

K

Kenneth McDonald

For debugging purposes, I'd like to be able to do something like

print [1,2,3]

and have "[1,2,3]" printed to the screen, not 123. How do I go about
this. I've experimented with to_s and puts, and not found anything
obvious.

Thanks,
Ken
 
L

Li Chen

Kenneth said:
For debugging purposes, I'd like to be able to do something like

print [1,2,3]

and have "[1,2,3]" printed to the screen, not 123. How do I go about
this. I've experimented with to_s and puts, and not found anything
obvious.

Thanks,
Ken

C:\Documents and Settings\chen73>irb
irb(main):001:0> a=[1,2,3]
=> [1, 2, 3]
irb(main):002:0> p a
[1, 2, 3]
=> nil
irb(main):003:0>

Li
 
T

Thomas B.

Kenneth said:
For debugging purposes, I'd like to be able to do something like

print [1,2,3]

and have "[1,2,3]" printed to the screen, not 123. How do I go about
this. I've experimented with to_s and puts, and not found anything
obvious.

Thanks,
Ken

Each object has method inspect and it does the trick. The irb in fact
prints the result's inspect to show it to you.

["a",:b,3].inspect #=> "[\"a\", :b, 3]"

TPR.
 
D

Dido Sevilla

T24gV2VkLCBTZXAgMjQsIDIwMDggYXQgMTI6NDkgQU0sIEtlbm5ldGggTWNEb25hbGQKPGtlbm5l
dGgubS5tY2RvbmFsZEBzYmNnbG9iYWwubmV0PiB3cm90ZToKPiBGb3IgZGVidWdnaW5nIHB1cnBv
c2VzLCBJJ2QgbGlrZSB0byBiZSBhYmxlIHRvIGRvIHNvbWV0aGluZyBsaWtlCj4KPiBwcmludCBb
MSwyLDNdCj4KPiBhbmQgaGF2ZSAiWzEsMiwzXSIgcHJpbnRlZCB0byB0aGUgc2NyZWVuLCBub3Qg
MTIzLiBIb3cgZG8gSSBnbyBhYm91dCB0aGlzLgo+IEkndmUgZXhwZXJpbWVudGVkIHdpdGggdG9f
cyBhbmQgcHV0cywgYW5kIG5vdCBmb3VuZCBhbnl0aGluZyBvYnZpb3VzLgoKcCBbMSwyLDNdCnB1
dHMgWzEsMiwzXS5pbnNwZWN0CgpUaGUgbGFzdCBpcyB1c2VmdWwgaWYgeW91IHdhbnQgdG8gZ2V0
IHRoZSBzdHJpbmcgcmVwcmVzZW50YXRpb24gb2YgdGhlClJ1Ynkgb2JqZWN0LCBlLmcuIGlmIHlv
dSdyZSB1c2luZyBhIGxvZ2dlciwgc28geW91IGNhbiBkbyBzb21ldGhpbmcKbGlrZToKCmxvZy5k
ZWJ1ZyhvYmouaW5zcGVjdCkKCi0tIArmma7pgJrjgZjjgoPjgarjgYTjga7jgYzlvZPnhLbjgarj
gonnrZTjgYjjgovnp4Hjga/kvZXjgYzjgafjgY3jgovvvJ8K5pmu6YCa44Gn44KC5pmu6YCa44GY
44KD44Gq44GP44Gm5oSf44GY44KL44G+44G+5oSf44GY44KL44GT44Go44Gg44GR44KS44GZ44KL
44KI77yBCmh0dHA6Ly9zdG9ybXd5cm0uYmxvZ3Nwb3QuY29tCg==
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top