简体   繁体   English

循环遍历数组 Ruby

[英]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?有什么我想念的吗?

It doesn't exist a constructor of Array that takes a variable number of arguments.它不存在采用可变数量参数的Array构造函数。 You can do it你能行的

contacts = [arg1, arg2, arg3, arg4]

And then, you can iterate with the each method然后,您可以使用each方法进行迭代

contacts.each do |contact|
  puts contact
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM