iterate through an @Array

J

John William

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 "<table width='1000' border='1' cellspacing='3' cellpadding='0'>"
puts"<tr><td>#{t.name}</td><td>#{t.address}</td><td>#{t.city}</td><td>#{t.state}</td></
tr>"
puts “</table>â€
end
 
H

Hassan Schroeder

How do I display the attr_accessors in a table
This doesn=92t seem to be the right way as it repeats the table each time
it iterates through the array.

puts "<table width=3D'1000' border=3D'1' cellspacing=3D'3' cellpadding=3D'=
0'>"
@sample_array.each do |t|
puts"<tr><td>#{t.name}</td><td>#{t.address}</td><td>#{t.city}</td><td>#{t=
state} said:
puts =93</table>=94

--=20
Hassan Schroeder ------------------------ (e-mail address removed)
twitter: @hassan
 
R

Rob Kaufman

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 "<table width=3D'1000' border=3D'1' cellspacing=3D'3' cellpadding=3D'0=
'>"

@sample_array.each do |t|
# Do for every item in the array (make one row per item)
puts"<tr><td>#{t.name}</td><td>#{t.address}</td><td>#{t.city}</td><td>#{t=
state}</td></tr>"
end

# Do once
puts =93</table>=94

New to ruby
If I have an Array

@sample_array

How do I display the attr_accessors in a table
This doesn=92t 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 "<table width=3D'1000' border=3D'1' cellspacing=3D'3' cellpadding=3D= '0'>"
puts"<tr><td>#{t.name}</td><td>#{t.address}</td><td>#{t.city}</td><td>#{t=
state} said:
tr>"
puts =93</table>=94
end



--=20
Rob Kaufman
http://notch8.com
gtalk/jabber/email: (e-mail address removed)
p: 858-412-7749
f: 866-477-1620
 
J

John William

Rob said:
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 "<table width='1000' border='1' cellspacing='3' cellpadding='0'>"

@sample_array.each do |t|
# Do for every item in the array (make one row per item)
puts"<tr><td>#{t.name}</td><td>#{t.address}</td><td>#{t.city}</td><td>#{t.state}</td></tr>"
end

# Do once
puts �</table>�

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

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top