Created
October 4, 2011 14:12
-
-
Save pasberth/1261738 to your computer and use it in GitHub Desktop.
2つ以上のEnumerableを同時に繰り返す
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- 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