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

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

Constructor Details

#initialize(*args) ⇒ Chain

call-seq:

Enumerator::Chain.new(*enums) -> enumerator_chain

Generates a new enumerator which iterates over each one of the given enumerable objects in sequence.

e = Enumerator::Chain.new(1..3, [4, 5])
e.to_a  #=> [1, 2, 3, 4, 5]


50
51
52
53
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 50

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

Instance Method Details

#+(other) ⇒ Object

call-seq:

chain + other_enum -> enumerator_chain

Returns a new Enumerator::Chain object which will enumerate over the elements of this chain, followed by the elements of other_enum.

e1 = Enumerator::Chain.new(1..3, [4, 5])
e2 = e1 + [6, 7]
e2.to_a  #=> [1, 2, 3, 4, 5, 6, 7]


132
133
134
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 132

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

#each(&block) ⇒ Object

call-seq:

chain.each { |obj| block } -> chain
chain.each                 -> enumerator

Iterates over the elements of the first enumerable by calling the each method on it with the given block, then proceeds to the next enumerable in the chain and continues until the end.

e = Enumerator::Chain.new(1..3, [4, 5])
e.each { |x| puts x }
# prints: 1, 2, 3, 4, 5


68
69
70
71
72
73
74
75
76
77
78
79
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 68

def each(&block)
  return to_enum(:each) unless block

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

  self
end

#inspectObject

call-seq:

chain.inspect -> string

Returns a printable version of the enumerator chain.

Enumerator::Chain.new(1..3, [4, 5]).inspect
#=> "#<Enumerator::Chain: [1..3, [4, 5]]>"


145
146
147
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 145

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

#rewindObject

call-seq:

chain.rewind -> chain

Rewinds the enumerator chain by calling the rewind method on each enumerable that has been iterated, in reverse order. Each enumerable that defines a rewind method will be rewound.

e = Enumerator::Chain.new((1..3), [4, 5])
e.next  #=> 1
e.rewind
e.next  #=> 1


111
112
113
114
115
116
117
118
119
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 111

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

  self
end

#sizeObject

call-seq:

chain.size -> integer or nil

Returns the total size of the enumerator chain if all of the chained enumerables define size. Otherwise it returns nil.

Enumerator::Chain.new(1..3, [4, 5]).size  #=> 5
Enumerator::Chain.new(1..3, loop).size    #=> nil


91
92
93
94
95
96
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 91

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