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

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

···

On Thu, 23 Feb 2006 junk5@microserf.org.uk wrote:

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

--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama

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

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

···

On Thu, 2006-02-23 at 02:48 +0900, junk5@microserf.org.uk wrote:

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.

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk

Hi,

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

Pete

···

On 22/02/06, junk5@microserf.org.uk <junk5@microserf.org.uk> wrote:

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

junk5@microserf.org.uk wrote:

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

Thanks all!