Ruby 如何通过跳过先前引用的元素的数组循环?

Ruby 如何通过跳过先前引用的元素的数组循环?,ruby,arrays,Ruby,Arrays,有没有办法循环两个数组,让它们每次引用某个元素?例如: first_array = ['a', 'b', 'c', 'd'] second_array = ['e', 'f', 'g', 'h'] first_array.each |item| puts item.get(second_array) 结果将类似于: a would work with e b would work with f c would work with g d would work with h 我试图这

有没有办法循环两个数组,让它们每次引用某个元素?例如:

first_array = ['a', 'b', 'c', 'd']
second_array = ['e', 'f', 'g', 'h']

first_array.each |item| puts item.get(second_array)
结果将类似于:

a would work with e
b would work with f
c would work with g    
d would work with h

我试图这样做,当第一个数组中的一个变量被传递给第二个数组时,它会移动到第二个数组中的下一个变量,跳过以前使用的变量。

您可以这样做:

first_array.each_with_index { |v, i| puts "#{v} can work with #{second_array[i]}" }
# a would work with e
# b would work with f
# c would work with g
# d would work with h
那是拉链


给定的第二个数组总是至少和第一个数组一样长

first_array.each_with_index |item,index| do
  puts "#{item} would work with #{second_array[index]}"
end

假设两个数组的大小相同,如您的示例所示,您可以使用:

无论何时,当您使用此处[a1,a2]大小相同的数组a,并希望对每个i操作[a[0][i]、a[1][i]、…]时,您始终可以选择使用转置或。从某种意义上说,它们是

您也可以只使用索引:

a1.each_index { |i] puts "#{a1[i]} and #{a2[i]}" }

我不熟悉Ruby,但我会使用循环结构提取这些数据,循环结构中有一个计数器,其长度等于第一个数组的总长度。然后我会从每个数组中提取计数器处的值。我不太确定你的意思:\n基本上是Ruby语法在Neathoh下概述的其他值好的,我现在要研究一下,谢谢:okie doke,你是对的,所以我会做得更好:嗯,年轻人的反应。我想我明白你的意思了,我要试试,谢谢!很好,从来没有在函数列表中排得那么远…:我会让索引在循环中有一个+=1吗?ruby会帮你做的。okie doke,谢谢:嘿,你知道soundcloud api的分页吗?我正在尝试使用linked_partitioning=1值以及next href。。或者你能传播这个消息吗?:\@托尼·霍普金森不,不是我,我应该考虑另一个问题。
a1 = %w{a b c d}
  #=> ['a', 'b', 'c', 'd']  
a2 = %w{e f g h}
  #=> ['e', 'f', 'g', 'h']
[a1,a2].transpose { |s1,s2| puts "#{s1} and #{s2}" }
  #-> a and e
  #   b and f
  #   c and g
  #   d and h
a1.each_index { |i] puts "#{a1[i]} and #{a2[i]}" }