Enumerable(T)
The Enumerable mixin provides collection classes with several traversal, searching,
filtering and querying methods.
Including types must provide an each method, which yields successive members
of the collection.
For example:
class Three
include Enumerable(Int32)
def each(&)
yield 1
yield 2
yield 3
end
end
three = Three.new
three.to_a # => [1, 2, 3]
three.select &.odd? # => [1, 3]
three.all? { |x| x < 10 } # => true
Note that most search and filter methods traverse an Enumerable eagerly,
producing an Array as the result. For a lazy alternative refer to
the Iterator and Iterable modules.
Instance methods
find_value(fallback = nil, &)
Yields each value until the first truthy block result and returns that result.
Accepts an optional parameter if_none, to set what gets returned if
no element is found (defaults to nil).
[1, 2, 3, 4].find_value { |i| i > 2 } # => true
[1, 2, 3, 4].find_value { |i| i > 8 } # => nil
[1, 2, 3, 4].find_value(-1) { |i| i > 8 } # => -1