# Iterate through an @Array

**URL:** <https://www.ruby-forum.com/t/iterate-through-an-array/184960>\
**Category:** Ruby\
**Created:** [March 11, 2010, 1:35am UTC](https://www.ruby-forum.com/t/iterate-through-an-array/184960 "2010-03-11T01:35:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 11, 2010, 1:35am UTC](https://www.ruby-forum.com/t/iterate-through-an-array/184960/1 "2010-03-11T01:35:48Z")

</div>

New to ruby  
If I have an Array

@sample\_array

How do I display the attr\_accessors in a table  
This doesnâ€™t seem to be the right way as it repeats the table each time  
it iterates through the array.

Thanks,

John

@sample\_array.each do |t|  
puts “

”  
puts"\</  
tr\>"  
puts â€œ

| #{t.name} | #{t.address} | #{t.city} | #{t.state} |

â€  
end

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 11, 2010, 1:55am UTC](https://www.ruby-forum.com/t/iterate-through-an-array/184960/2 "2010-03-11T01:55:45Z")

</div>

On Wed, Mar 10, 2010 at 4:35 PM, John W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How do I display the attr\_accessors in a table  
> This doesn’t seem to be the right way as it repeats the table each time  
> it iterates through the array.

puts “

”

> @sample\_array.each do |t|  
> puts"

"  
end  
puts “

| #{t.name} | #{t.address} | #{t.city} | #{t.state} |

”

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 11, 2010, 1:56am UTC](https://www.ruby-forum.com/t/iterate-through-an-array/184960/3 "2010-03-11T01:56:20Z")

</div>

Hey John,  
You just need to move the table start and end outside of the each  
statement. Everything inside the each block gets done for each item  
in the array:

# Do once

puts “

”

@sample\_array.each do |t|

# Do for every item in the array (make one row per item)

puts"

"  
end
# Do once

puts “

| #{t.name} | #{t.address} | #{t.city} | #{t.state} |

”

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 11, 2010, 2:58am UTC](https://www.ruby-forum.com/t/iterate-through-an-array/184960/4 "2010-03-11T02:58:17Z")

</div>

Rob K. wrote:

> Hey John,  
> You just need to move the table start and end outside of the each  
> statement. Everything inside the each block gets done for each item  
> in the array:
> 
> # Do once
> 
> puts “
> 
> ”
> 
> @sample\_array.each do |t|
> 
> # Do for every item in the array (make one row per item)
> 
> puts"
> 
> "  
> end
> # Do once
> 
> puts ï¿½
> 
> | #{t.name} | #{t.address} | #{t.city} | #{t.state} |
> 
> ï¿½

Thank you all for your help. It makes sense now and works perfect!

John
