How does one get a String representation of an Array (not to_s)?

J

junk5

Hi all

(I know about to_s --- that isn't what this post is about!)

Say I have an array that I create with:

a = [1, 2, [3, 4], 5]

At the irb prompt, Ruby replies:

=> [1, 2, [3, 4], 5]

So how do I get a String containing "[1, 2, [3, 4], 5]"? Doing a.to_s
gives me "12345", which is not what I want.

I need this for debugging purposes and would rather not have to write
my own array printer. It must be easy, but I can't see which function I
should use.

Thanks in advace,

C
 
A

ara.t.howard

Hi all

(I know about to_s --- that isn't what this post is about!)

Say I have an array that I create with:

a = [1, 2, [3, 4], 5]

At the irb prompt, Ruby replies:

=> [1, 2, [3, 4], 5]

So how do I get a String containing "[1, 2, [3, 4], 5]"? Doing a.to_s
gives me "12345", which is not what I want.

I need this for debugging purposes and would rather not have to write
my own array printer. It must be easy, but I can't see which function I
should use.

Thanks in advace,

C

inspect

-a
 
R

Ross Bamford

Hi all

(I know about to_s --- that isn't what this post is about!)

Say I have an array that I create with:

a = [1, 2, [3, 4], 5]

At the irb prompt, Ruby replies:

=> [1, 2, [3, 4], 5]

So how do I get a String containing "[1, 2, [3, 4], 5]"? Doing a.to_s
gives me "12345", which is not what I want.

s = [1,2,[3,4],5].inspect
# => "[1, 2, [3, 4], 5]"

puts s
[1, 2, [3, 4], 5]
 
R

Robert Klemme

Hi all

(I know about to_s --- that isn't what this post is about!)

Say I have an array that I create with:

a = [1, 2, [3, 4], 5]

At the irb prompt, Ruby replies:

=> [1, 2, [3, 4], 5]

So how do I get a String containing "[1, 2, [3, 4], 5]"? Doing a.to_s
gives me "12345", which is not what I want.

I need this for debugging purposes and would rather not have to write
my own array printer. It must be easy, but I can't see which function
I should use.

Thanks in advace,

C

puts a.inspect

Or just

p a

Kind regards

robert
 
1

1337p337

Hi,

a.inspect is probably what you're looking for. 'p a' will print it
directly to stdout.

Pete
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top