Iterate through an @Array

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

On Wed, Mar 10, 2010 at 4:35 PM, John W. [email protected]
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}

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}

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs