def chain(*args)
Enumerator::Chain.new(self, *args)
end
+end
+class Enumerator
def +(other)
- Enumerator::Chain.new(self, other)
+ Chain.new(self, other)
end
-end
-class Enumerator
class Chain
include Enumerable
def initialize(*args)
- @enums = args
- end
-
- def initialize_copy(orig)
- @enums = orig.__copy_enums
+ @enums = args.freeze
+ @pos = -1
end
def each(&block)
- return to_enum unless block_given?
+ return to_enum unless block
- @enums.each { |e| e.each(&block) }
+ i = 0
+ while i < @enums.size
+ @pos = i
+ @enums[i].each(&block)
+ i += 1
+ end
self
end
end
def rewind
- @enums.reverse_each do |e|
+ while 0 <= @pos && @pos < @enums.size
+ e = @enums[@pos]
e.rewind if e.respond_to?(:rewind)
+ @pos -= 1
end
self
end
- def inspect
- "#<#{self.class}: #{@enums.inspect}>"
+ def +(other)
+ self.class.new(self, other)
end
- def __copy_enums
- @enums.each_with_object([]) do |e, a|
- a << e.clone
- end
+ def inspect
+ "#<#{self.class}: #{@enums.inspect}>"
end
end
end