# Returning p array

**URL:** <https://www.ruby-forum.com/t/returning-p-array/54972>\
**Category:** Ruby\
**Created:** [March 15, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/returning-p-array/54972 "2006-03-15T18:04:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 15, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/returning-p-array/54972/1 "2006-03-15T18:04:15Z")

</div>

I like the way in which p prints out the contents of an array. It’s easy  
for me to read. Is it possible to make a method return p array? Or is  
there a better way to do this?

def info  
#create an array  
p array  
return p array  
end

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 15, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/returning-p-array/54972/2 "2006-03-15T18:04:38Z")

</div>

rtilley a écrit :

> I like the way in which p prints out the contents of an array. It’s  
> easy for me to read. Is it possible to make a method return p array?  
> Or is there a better way to do this?
> 
> def info  
> #create an array  
> p array  
> return p array  
> end  
> “p” just uses the “inspect” method. Thus you can do that :

def info

# create an array

p array  
array.inspect  
end

Pierre

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 15, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/returning-p-array/54972/3 "2006-03-15T18:11:03Z")

</div>

rtilley wrote:

> I like the way in which p prints out the contents of an array. It’s easy  
> for me to read. Is it possible to make a method return p array? Or is  
> there a better way to do this?
> 
> def info  
> #create an array  
> p array  
> return p array  
> end

Just to clarify my question… When I create an array like this:

x = Array.new  
x.push(1,2,3)

And then write the array to a file, I get a file that reads 123. I would  
like a file that looks like this [1, 2, 3] instead.

Thank you,  
Brad

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 15, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/returning-p-array/54972/4 "2006-03-15T18:20:27Z")

</div>

Pierre Barbier de Reuille wrote:

> “p” just uses the “inspect” method. Thus you can do that :
> 
> def info
> 
> # create an array
> 
> p array  
> array.inspect  
> end
> 
> Pierre

Thank you Pierre and Robert. That is exactly what I wanted to do.

Brad

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 15, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/returning-p-array/54972/5 "2006-03-15T18:11:19Z")

</div>

“rtilley” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:dv9h29$agf$removed\_email\_address@domain.invalid…

> I like the way in which p prints out the contents of an array. It’s easy  
> for me to read. Is it possible to make a method return p array? Or is  
> there a better way to do this?
> 
> def info  
> #create an array  
> p array  
> return p array  
> end

This won’t work because p returns nil. Do this

def info  
create\_array\_somehow.inspect  
end

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![rtilley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@rtilley](https://www.ruby-forum.com/u/rtilley)\
**Post date:** [March 16, 2006, 10:30am UTC](https://www.ruby-forum.com/t/returning-p-array/54972/6 "2006-03-16T10:30:14Z")

</div>

“rtilley” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:dv9hej$dnv$removed\_email\_address@domain.invalid…

> Just to clarify my question… When I create an array like this:
> 
> x = Array.new  
> x.push(1,2,3)

This can be shortened to

x=[1,2,3]

> And then write the array to a file, I get a file that reads 123. I would  
> like a file that looks like this [1, 2, 3] instead.

Do x.inspect or x.join ", "

Kind regards

```
robert

```
