Kenneth Kunz shined a light on the SyncEnumerator class, which comes with the standard library. A little bit of slightly life-changing code I’d say.
require 'generator'
numbers = (1..5)
letters = ('a'..'c')
enum = SyncEnumerator.new( numbers, letters )
enum.each { |num, let| print "#{num},#{let} " }
Which prints: 1,a 2,b 3,c 4, 5,.
The each iterates through both collections simultaneously. You can pass any number of collections into SyncEnumerator. And, naturally, you can use any of the normal set of Enumerable methods. And you will.
require 'generator'
class Sync < SyncEnumerator
def self.[]( *args ); new( *args ); end
end
Sync[numbers,letters].map { |n,l| "#{n} #{l}" }.join "\n"
Read: Enumerate Side-by-side with SyncEnumerator