Enumerable is the mix-in module for the enumeration. The
including class must provide the method each
. All
methods provided by Enumerable
are defined using
each
.
collect {|item|...}
Returns an array of the result of the iterator block evaluation over each item.
find {|item|...}
Returns the first item which satisfies the iterator block condition.
find_all {|item|...}
Returns an array of all items which satisfy the iterator block condition.
grep(pattern)
grep(pattern) {|item|...}
Returns an array of all items which satisfy
`item === pattern
'.
If calls as an iterator, grep
evaluates the iterator
block over every item matched.
member?(val)
Returns true if there is an item which equals to val.
Comparison is done by the operator `==
'.
index(val)
Returns the index of the item which equals to val using
operator `==
'. The index of the first item is 0.
Returns nil
if there is no matching item. It is
meaningless for unordered enumerables.
length
size
Returns the number of items.
min
Returns the smallest item assuming all items are
Comparable
.
min{|a, b|...}
Returns the smallest item using the evaluated value of the iterator block.
max
Returns the greatest item assuming all items are
Comparable
.
max{|a, b|...}
Returns the greatest item using the evaluated value of the iterator block.
reverse
Returns the array of the items in reverse order.
sort
sort {|a, b|...}
Returns the sorted array of the items. If the iterator block is
given, it must compare two items just like <=>
.
to_a
Converts an Enumerable to an array.