module Enumerable(T)
Overview
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.
Direct including types
Defined in:
crystal_on_steroids/enumerable.crInstance Method Summary
-
#avg
Returns the average of a collection of numbers.
-
#excludes?(obj)
Returns
true
if the collection does not contains obj,false
otherwise. -
#many?(&)
Returns
true
if many elements fulfilled the block condition, much like any?, sopeople.many? { |p| p.age > 26 }
returnstrue
if more than one person is over 26. -
#many?
Returns
true
if the enumerable has more than 1 element. -
#pluck(*keys)
Convert an enumerable to an array based on the given key.
-
#without(*items)
Returns a copy of the enumerable without the specified items.
Instance Method Detail
Returns true
if the collection does not contains obj, false
otherwise.
[1, 2, 3].excludes?(4) # => true
[1, 2, 3].excludes?(3) # => false
source: Rails ActiveSupport
Returns true
if many elements fulfilled the block condition,
much like any?, so people.many? { |p| p.age > 26 }
returns true
if more than one person is over 26.
source: Rails ActiveSupport
Returns true
if the enumerable has more than 1 element. functionally
equivalent to enum.to_a.size > 1
.
source: Rails ActiveSupport
Convert an enumerable to an array based on the given key.
[{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name)
=> ["David", "Rafael", "Aaron"]
[{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pluck(:id, :name)
=> [{1, "David"}, {2, "Rafael"}]
source: Rails ActiveSupport
Returns a copy of the enumerable without the specified items.
[1, 2, 3, 4, 5].without 3, 5
=> [1, 2, 4]
source: Rails ActiveSupport