# How does one get a String representation of an Array (not to

**URL:** <https://www.ruby-forum.com/t/how-does-one-get-a-string-representation-of-an-array-not-to/71928>\
**Category:** Ruby\
**Created:** [February 22, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/how-does-one-get-a-string-representation-of-an-array-not-to/71928 "2006-02-22T18:50:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 22, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/how-does-one-get-a-string-representation-of-an-array-not-to/71928/1 "2006-02-22T18:50:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 22, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/how-does-one-get-a-string-representation-of-an-array-not-to/71928/2 "2006-02-22T18:56:21Z")

</div>

On Thu, 23 Feb 2006 removed\_email\_address@domain.invalid wrote:

> =\> [1, 2, [3, 4], 5]  
> C  
> inspect

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 22, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/how-does-one-get-a-string-representation-of-an-array-not-to/71928/3 "2006-02-22T18:56:22Z")

</div>

On Thu, 2006-02-23 at 02:48 +0900, removed\_email\_address@domain.invalid wrote:

> =\> [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]

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 22, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/how-does-one-get-a-string-representation-of-an-array-not-to/71928/4 "2006-02-22T18:56:22Z")

</div>

Hi,

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

Pete

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 22, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/how-does-one-get-a-string-representation-of-an-array-not-to/71928/5 "2006-02-22T19:05:33Z")

</div>

Thanks all!

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 22, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/how-does-one-get-a-string-representation-of-an-array-not-to/71928/6 "2006-02-22T18:59:28Z")

</div>

removed\_email\_address@domain.invalid wrote:

> =\> [1, 2, [3, 4], 5]  
> C  
> puts a.inspect

Or just

p a

Kind regards

```
robert
```
