What's the 'Ruby way' to iterate over two arrays at once

后端 未结 7 431
别跟我提以往
别跟我提以往 2020-12-04 05:52

More of a syntax curiosity than a problem to solve...

I have two arrays of equal length, and want to iterate over them both at once - for example, to output both the

相关标签:
7条回答
  • 2020-12-04 06:23

    In addition to a.zip(b).each{|x,y| } as others have said, you can also say [a,b].transpose.each{|x,y| }, which strikes me as a tiny bit more symmetrical. Probably not as fast, though, since you're creating the extra [a,b] array.

    0 讨论(0)
提交回复
热议问题