source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-enum-chain/mrblib/chain.rb@ 439

Last change on this file since 439 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 977 bytes
Line 
1##
2# chain.rb Enumerator::Chain class
3# See Copyright Notice in mruby.h
4
5module Enumerable
6 def chain(*args)
7 Enumerator::Chain.new(self, *args)
8 end
9end
10
11class Enumerator
12 def +(other)
13 Chain.new(self, other)
14 end
15
16 class Chain
17 include Enumerable
18
19 def initialize(*args)
20 @enums = args.freeze
21 @pos = -1
22 end
23
24 def each(&block)
25 return to_enum unless block
26
27 i = 0
28 while i < @enums.size
29 @pos = i
30 @enums[i].each(&block)
31 i += 1
32 end
33
34 self
35 end
36
37 def size
38 @enums.reduce(0) do |a, e|
39 return nil unless e.respond_to?(:size)
40 a + e.size
41 end
42 end
43
44 def rewind
45 while 0 <= @pos && @pos < @enums.size
46 e = @enums[@pos]
47 e.rewind if e.respond_to?(:rewind)
48 @pos -= 1
49 end
50
51 self
52 end
53
54 def +(other)
55 self.class.new(self, other)
56 end
57
58 def inspect
59 "#<#{self.class}: #{@enums.inspect}>"
60 end
61 end
62end
Note: See TracBrowser for help on using the repository browser.