Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created October 4, 2011 14:12
Show Gist options
  • Select an option

  • Save pasberth/1261738 to your computer and use it in GitHub Desktop.

Select an option

Save pasberth/1261738 to your computer and use it in GitHub Desktop.
2つ以上のEnumerableを同時に繰り返す
# -*- coding: utf-8 -*-
class SynchronizedIterator
include Enumerable
def initialize *enumerables
@enumerables = enumerables.map do |e|
e.map { |item| item }
end
end
def each &blk
return self unless blk
@enumerables.first.length.times do |i|
blk.call *@enumerables.map { |e| e[i] }
end
self
end
end
module Enumerable
def sync_each *enumerables, &blk
SynchronizedIterator.new(self, *enumerables).each &blk
end
end
["a", "b", "c"].sync_each [1, 2, 3] do |char, num|
puts "#{char} -> #{num}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment