Class: Enumerator::Chain

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
mrbgems/mruby-enum-chain/mrblib/chain.rb

Constant Summary

Constants included from Enumerable

Enumerable::NONE

Instance Method Summary collapse

Methods included from Enumerable

__update_hash, #all?, #any?, #chain, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_slice, #each_with_index, #each_with_object, #entries, #filter_map, #find_all, #find_index, #first, #flat_map, #grep, #group_by, #hash, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #sort, #sort_by, #take, #take_while, #tally, #to_h, #uniq, #zip

Constructor Details

#initialize(*args) ⇒ Chain

Returns a new instance of Chain



19
20
21
22
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 19

def initialize(*args)
  @enums = args.freeze
  @pos = -1
end

Instance Method Details

#+(other) ⇒ Object



54
55
56
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 54

def +(other)
  self.class.new(self, other)
end

#each(&block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 24

def each(&block)
  return to_enum unless block

  i = 0
  while i < @enums.size
    @pos = i
    @enums[i].each(&block)
    i += 1
  end

  self
end

#inspectObject



58
59
60
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 58

def inspect
  "#<#{self.class}: #{@enums.inspect}>"
end

#rewindObject



44
45
46
47
48
49
50
51
52
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 44

def rewind
  while 0 <= @pos && @pos < @enums.size
    e = @enums[@pos]
    e.rewind if e.respond_to?(:rewind)
    @pos -= 1
  end

  self
end

#sizeObject



37
38
39
40
41
42
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 37

def size
  @enums.reduce(0) do |a, e|
    return nil unless e.respond_to?(:size)
    a + e.size
  end
end