Iterate through Nested Array - Noob question

I am new to ruby, I am trying to populate data into a form using a set
of variables in nested loops

For i do 1..2 do
  project = @blah.create(:project, :blah =>......

The above is just a spinet, but works fine

For j in 1..4 do
owner = ["tim", "alice", "candy"]
status = ["new", "current", "out_of_date", "banned"]
  contact = @contact.create(:contact, :owner => owner, :status =>
status, :project = project)

If I add:
current_owner = owner[(j-1) % owner.size] it just uses the first
variable over and over again

What I am trying to accomplish:
Create two new projects
For each project I am creating 4 new forms
For each form I want to rotate through the variables for owner and
status. If I reach the end of a list of variables (only 3 for owners). I
want it to repeat through the list.

The other scenario:

For i do 1..10 do
  project = @blah.create(:project, :blah =>......

For j in 1..4 do
owner = ["tim", "alice", "candy"]
status = ["new", "current", "out_of_date", "banned"]
  contact = @contact.create(:contact, :owner => owner, :status =>
status)

QUESTION: By having more projects than data for owners? do I have to do
anything special to ensure it continues to loop through owner and status
until i is done?

···

--
Posted via http://www.ruby-forum.com/.

I am new to ruby, I am trying to populate data into a form using a set
of variables in nested loops

For i do 1..2 do
project = @blah.create(:project, :blah =>......

The above is just a spinet, but works fine

For j in 1..4 do
owner = ["tim", "alice", "candy"]
status = ["new", "current", "out_of_date", "banned"]
contact = @contact.create(:contact, :owner => owner, :status =>
status, :project = project)

If I add:
current_owner = owner[(j-1) % owner.size] it just uses the first
variable over and over again

Did you also update the hash where you specify who is the owner? (something
to consider: in the above code, you pass an Array here, but now that you
have selected a desired owner, you have a String. Since I don't know what
code you are working with, I can't check for you that this is acceptable).

Also, if you iterate over 0..3, then you don't have to subtract 1 from j.
Also, I think you might be expecting that to iterate over 1,2,3 but two dots
is inclusive, so 1..4 will iterate over 1,2,3,4 I suspect you actually want
0..2 or, using three dots, which is exclusive, 0...3 either of which will
iterate over 0,1,2, and then you won't have to subtract 1 from i before you
mod it.

The other scenario:

For i do 1..10 do
project = @blah.create(:project, :blah =>......

For j in 1..4 do
owner = ["tim", "alice", "candy"]
status = ["new", "current", "out_of_date", "banned"]
contact = @contact.create(:contact, :owner => owner, :status =>
status)

QUESTION: By having more projects than data for owners? do I have to do
anything special to ensure it continues to loop through owner and status
until i is done?

Right now, it is not iterating through status at all (it passes the entire
status array each time). With your above method of selecting an owner, it
does iterate through owner (though possibly incorrectly), and that is based
on the value of j, which is reset for each project. So assuming you are okay
with that, then no, you do not need to do anything special.

···

On Fri, Nov 12, 2010 at 12:52 PM, Dina W. <dinamcc@yahoo.com> wrote: