# Displaying an array

**URL:** <https://www.ruby-forum.com/t/displaying-an-array/113332>\
**Category:** Ruby\
**Created:** [August 29, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/displaying-an-array/113332 "2007-08-29T23:09:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [August 29, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/displaying-an-array/113332/1 "2007-08-29T23:09:04Z")

</div>

In irb, an array displays like this:

irb(main):001:0\> a = %w(bird dog cat)  
=\> [“bird”, “dog”, “cat”]  
irb(main):002:0\>

Is there a command to get the same format when I display an array with a  
program?

## r1test.rb:

a1 = %w(cat dog bird)  
puts a1

$ ruby r1test.rb  
cat  
dog  
bird

Also, this code:

* * *

a1 = %w(cat dog bird)  
puts a1

puts

a2 = %w{john joe jim}  
puts a2

## –output:– cat dog bird john joe jim

seems to suggest that braces and parentheses are equivalent. Is that  
the case?

Thanks

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [August 29, 2007, 11:23pm UTC](https://www.ruby-forum.com/t/displaying-an-array/113332/2 "2007-08-29T23:23:32Z")

</div>

7stud 7stud wrote:

> In irb, an array displays like this:
> 
> irb(main):001:0\> a = %w(bird dog cat)  
> =\> [“bird”, “dog”, “cat”]  
> irb(main):002:0\>
> 
> Is there a command to get the same format when I display an array with a  
> program?

p (which is equivalent to puts foo.inspect)

> Also, this code:
> 
> a1 = %w(cat dog bird)  
> puts a1
> 
> a2 = %w{john joe jim}  
> puts a2  
> […]  
> seems to suggest that braces and parentheses are equivalent. Is that  
> the case?

You can use any non-alphanumeric character with %w (or %r or whatever).  
It  
won’t make a difference. Examples:  
%w  
%w\_li la lo\_  
%w#chunky bacon#  
%w)foo bar) # This one’s strange, I know.  
etc.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [August 30, 2007, 12:18am UTC](https://www.ruby-forum.com/t/displaying-an-array/113332/3 "2007-08-30T00:18:21Z")

</div>

7stud 7stud wrote:

> In irb, an array displays like this:
> 
> irb(main):001:0\> a = %w(bird dog cat)  
> =\> [“bird”, “dog”, “cat”]  
> irb(main):002:0\>
> 
> Is there a command to get the same format when I display an array with a  
> program?

C:\>type p.rb  
a = %w(cat dog bird)  
p a

C:\>ruby p.rb  
[“cat”, “dog”, “bird”]

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [August 30, 2007, 6:30am UTC](https://www.ruby-forum.com/t/displaying-an-array/113332/4 "2007-08-30T06:30:40Z")

</div>

Sebastian H. wrote:

> > Is there a command to get the same format when I display an array with a  
> > program?
> 
> p (which is equivalent to puts foo.inspect)
> 
> You can use any non-alphanumeric character with %w (or %r or whatever).  
> It won’t make a difference.

Thanks Sebastian. Thanks Gavin.
