Tom Tom - 1 year ago 75
Ruby Question

looping over array Ruby

I have an array:

contacts =, 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

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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download