Enumerable

A mix-in for repeating classes. This module's methods are all defined via each, so each must be defined in any class that includes this module.

Methods

all?
all? {|item| ... }

Returns true if all items are true. If any item is false, immediately returns FALSE.

When using a block, evaluates the block for each item and returns TRUE if all outcomes are true. If the block returns FALSE at any time, immediately returns FALSE.

p [1,2,3].all? {|v| v > 0}   # => true
p [1,2,3].all? {|v| v > 1}   # => false
any?
any? {|item| ... }

Returns FALSE if all items are false. If any item is true, immediately returns TRUE.

When using a block, evaluates the block for each item and returns FALSE if all outcomes are false. If the block returns TRUE at any time, immediately returns TRUE.

p [1,2,3].any? {|v| v > 3}   # => false
p [1,2,3].any? {|v| v > 1}   # => true
collect {|item| ... }

Returns an array that includes all the results of block evaluations for each item.

find {|item| ... }

Returns the first item that tested as true during the block evaluation. If no item was true, returns nil.

find_all {|item| ... }
select {|item| ... }

Returns an array of all items that tested as true during the block evaluation. If no item was true, returns an empty array.

include?(val)

Returns true if the list includes an item that satisfies the relationship of val and ==.

inject([init]) {|result, item| ... }

Passes the initial value init and the first item of self as arguments before executing the block. On the second and subsequent loops, passes the result of the previous block and the next item of self as arguments before executing the block. Iterates up to the last item and then returns the result of the final block.

Returns init if an item is empty.

If the initial value init is omitted, passes the first and second item to the block. If there is only one item in this case, the first item is returned without executing the block. Returns nil if an item is empty.

Example

Calculating the total:

p [1,2,3,4,5].inject(0) {|result, item| result + item }
  => 15

This is equivalent to the following code:

result = 0
[1,2,3,4,5].each {|v| result += v }
p result
=> 15
max

Returns the largest item. Assumes all items are comparable via the <=> method.

max {|a, b| ... }

Compares each item based on the block's evaluated value and returns the largest item.

Anticipates a block value that will be a positive integer if a>b, 0 if a==b, and a negative integer if a<b. If the block returns a non-integer, throws a TypeError exception.

max_by {|item| ... }

Compares block evaluation results via <=> and returns the largest item. Returns nil if there are no items.

min

Returns the smallest item. Assumes all items are comparable via the <=> method.

min {|a, b| ... }

Compares each item based on the block's evaluated value and returns the smallest item.

Anticipates a block value that will be a positive integer if a>b, 0 if a==b, and a negative integer if a<b. If the block returns a non-integer, throws a TypeError exception.

min_by {|item| ... }

Compares block evaluation results via <=> and returns the smallest item. Returns nil if there are no items.

sort
sort {|a, b| ... }

Creates and returns an array of all items sorted in ascending order.

When not using a block, calls the <=> for each item and sorts based upon those results.

To sort using other methods besides <=>, specify a block. The items will be sorted based on the evaluation of that block. Anticipates a block value that will be a positive integer if a>b, 0 if a==b, and a negative integer if a<b. If the block returns a non-integer, throws a TypeError exception.

sort_by {|item| ... }

Sorts self in ascending order by comparing block evaluation results via the <=> method. Creates and returns a new sorted array.

to_a
entries

Returns an array of all items.