Tom Tom - 6 months ago 8
Ruby Question

looping over array Ruby

I have an array:

contacts = Array.new(arg1, arg2, arg3, arg4)


And I want to create a loop that will take that array and fill in a field with that array like:

while contacts.index[0] < contacts.index[3]
fill_in('field', with: contacts)
...
contacts +=1
end


It tells me I've got the wrong number of arguments

ArgumentError: wrong number of arguments (4 for 0..2)


Is there something I'm missing?

Answer

It doesn't exist a constructor of Array that takes a variable number of arguments. You can do it

contacts = [arg1, arg2, arg3, arg4]

And then, you can iterate with the each method

contacts.each do |contact|
  puts contact
end
Comments