module

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

compact_blank
Source
exclude?(object) : Bool

Object#blank? cooperation for compact_blank.

Source
in_order_of(property : Symbol, values : Enumerable(V2)) : Array(T) forall V2
Source
index_by

Converts an Enumerable to a Hash by using the value returned by the block as the hash key. Be aware, if two elements return the same value as a key one will override the other. If you want to keep all values, then you should probably use group_by instead.

["Anna", "Ary", "Alice"].index_by { |e| e.size }
# => {4 => "Anna", 3 => "Ary", 5 => "Alice"}
["Anna", "Ary", "Alice", "Bob"].index_by { |e| e.size }
# => {4 => "Anna", 3 => "Bob", 5 => "Alice"}
Source
index_with
Source
many?
Source
many?
Source

Nested types