Added complete call-seq documentation for all enumerator chain methods in mrblib/chain.rb (8 methods): ## Enumerable Extension Methods: - chain: creates Enumerator::Chain from multiple enumerables for sequential iteration, enabling fluent chaining of enumerable objects ## Enumerator Extension Methods: - +: operator overload for creating chains from two enumerators, provides convenient syntax for combining enumerators ## Enumerator::Chain Class Methods: - new: constructor for creating chain from multiple enumerable arguments, stores enumerables and initializes position tracking ## Enumerator::Chain Instance Methods: - each: core iteration method that sequentially processes all chained enumerables, supports both block and enumerator return modes - size: calculates total size across all chained enumerables, returns nil if any enumerable doesn't support size method - rewind: resets iteration state by rewinding all previously iterated enumerables in reverse order, maintains proper state management - +: creates new chain by appending additional enumerable to existing chain, enables further composition of enumerator chains - inspect: provides debugging representation showing internal enumerable structure for development and troubleshooting Co-authored-by: Atlassian Rovo Dev
mruby-enum-chain
Description
This mrbgem provides the Enumerator::Chain class, which allows you to chain multiple enumerable objects together, treating them as a single, continuous enumerable.
This is useful when you have multiple collections (e.g., arrays, ranges, or other enumerators) and you want to iterate over all of them sequentially without first concatenating them into a single, larger collection.
How to Use
There are three main ways to create an Enumerator::Chain instance:
1. Using Enumerable#chain
You can call the chain method on any object that includes the Enumerable module.
a = [1, 2, 3]
b = (4..6)
c = {foo: 7, bar: 8}.each_key # Enumerator for keys
chained_enum = a.chain(b, c)
chained_enum.to_a # => [1, 2, 3, 4, 5, 6, :foo, :bar]
2. Using Enumerator#+
You can use the + operator on an Enumerator instance to chain it with another enumerable object.
enum1 = [1, 2].each
enum2 = %w(a b).each
chained_enum = enum1 + enum2
chained_enum.to_a # => [1, 2, "a", "b"]
# You can chain multiple times
enum3 = (10..11).each
chained_enum_2 = enum1 + enum2 + enum3
chained_enum_2.to_a # => [1, 2, "a", "b", 10, 11]
3. Using Enumerator::Chain.new
You can directly instantiate Enumerator::Chain by passing enumerable objects to its constructor.
arr = [10, 20]
rng = (30..31)
chained_enum = Enumerator::Chain.new(arr, rng)
chained_enum.to_a # => [10, 20, 30, 31]
Key Features
-
each(&block): Iterates through each element of the chained enumerables in the order they were added. Returns an enumerator if no block is given. -
size: Returns the total number of elements in all chained enumerables. If any of the chained enumerables do not respond tosize(e.g., an infinite enumerator or one with an unknown size), this method will returnnil.([1, 2].chain([3, 4])).size # => 4 ([1, 2].chain( (1..Float::INFINITY) )).size # => nil -
rewind: Rewinds all of the chained enumerables that respond to therewindmethod. This resets the iteration state to the beginning.e = [1,2].chain(3..4) e.next # => 1 e.next # => 2 e.next # => 3 e.rewind e.next # => 1 -
+(other): Creates a newEnumerator::Chainby appending another enumerable to the current chain.
License
MIT License