Ruby Enumerable Cheat Sheet

(inspired by David Grayson's Ruby 1.9.3 Enumerable module quick reference)

Basic Methods

Effect

Code Example

to_a
returns Array of all elements
to_h
Returns the result of interpreting enum as a list of [key, value] pairs.
  • count
  • count(value)
  • count { |x| ... }
  • Return total count of elements
  • Return count of elements equal to value
  • Return number of elements where the block returns true

Iteration

each { |x| ... }
Calls the given block once for each element, passing that element as a parameter to your code block.
each_with_index { |x, i| ... }
Calls the given block once for each element, passing that element and its indice as a parameter to your code block.
reverse_each { |x| ... }
Calls the given block once for each element in reverse order, passing that element as a parameter to your code block.
each_cons(n) { |x| ... }
Iterates the given block for each array of consecutive elements.
each_slice(n) { |x| ... }
Iterates the given block for each slice of elements.
cycle(times=nil) { |x| ... }
Calls block for each element of enum repeatedly n times or forever if none or nil is given. If a non-positive number is given or the collection is empty, does nothing. Returns nil if the loop has finished without getting interrupted.

Questions

  • include?(value)
  • member?(value)
Returns true if any element == value
  • all?
  • all { |a| ... }
  • Returns if none of the members are false or nil
  • Returns true if the block never returns false or nil
  • any?
  • any { |a| ... }
  • Returns true if at least one of the members is not false or nil
  • Returns true if the block ever returns a value other than false or nil
  • none?
  • none { |a| ... }
  • Returns true only if none of the members is true
  • Returns true if the block ever returns a value other than false or nil
  • one?
  • one { |a| ... }
  • Returns true only if exactly one of the members is true
  • Returns true if the block returns true only once

Sorting

  • sort
  • sort { |a, b| ... }
  • array sorted by <=> operator
  • The block must implement a comparison between a and b, and return -1, when a follows b, 0 when a and b are equivalent, or +1 if b follows a.
sort_by { |x| ... }
Returns array sorted by the return value of the block.
  • max
  • max(n)
  • max { |a, b| ... }
  • Returns maximum element based on sorting by { |a, b| a <=> b }
  • Returns maximum n elements are returned as an array.
  • Returns maximum element based on how code block sorts.
  • max_by { |x| ... }
  • max_by(n) { |x| ... }
  • Returns element with maximum block return value.
  • If the n argument is given, maximum n elements are returned as an array.
  • min
  • min(n)
  • min { |a, b| ... }
  • Returns minimum element based on sorting by { |a, b| a <=> b }
  • Returns minimum n elements are returned as an array.
  • Returns minimum element based on how code block sorts.
  • min_by { |x| ... }
  • min_by(n) { |x| ... }
  • Returns element with minimum block return value.
  • If the n argument is given, minimum n elements are returned as an array.
  • minmax
  • minmax { |a, b| ... }
  • Returns two elements array containing the minimum and the maximum value.
  • Returns two elements array containing the minimum and maximum value based on how your code block sorts.
minmax_by { |x| ... }
Returns two elements array containing the minimum and maximum value based on the min and max values of the criteria in your code block.

Searching for one element

  • detect(ifnone = nil) { |x| ... }
  • find(ifnone=nil) { |x| ... }
Returns the first for which the code block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil otherwise.
  • index(value=nil) or find_index(value=nil)
  • index { |x| ... } or find_index{ |x| ... }
  • Returns the index of the first object == to value, or nil if no match found
  • Returns the index of first object for which the code block is true, or nil otherwise

Filtering by value

  • find_all { |x| ... }
  • select { |x| ... }
returns array of all elements where block returns true
reject { |x| ... }
returns array of all elements where block returns false
  • grep(pattern)
  • grep(pattern) { |x| ... }
  • returns an array of every element for which pattern == element
  • If the optional block is supplied, each matching element is passed to it, and the block’s result is stored in the output array.

Filtering by position

  • first
  • first(n)
  • take(n)
  • returns the first element of the array
  • returns the first n elements of the array
  • returns the first n elements of the array
take_while { |x| ... }
Passes elements to the block until the block returns nil or false, then stops iterating and returns an array of all prior elements.
drop(n)
Drops first n elements and returns rest elements in an array.
drop_while { |x| ... }
Drops elements up to, but not including, the first element for which the block returns nil or false and returns an array containing the remaining elements.

Dividing into subsets

  • chunk { |x| ... }
  • chunk(init_state) { |x, state| ... }
Enumerates over the items, chunking them together based on the return value of the block. Consecutive elements which return the same block value are chunked together.
  • slice_before(pattern)
  • slice_before { |x| ... }
  • slice_before(init_state) { |x, state| ... }
  • Creates an enumerator for each chunked elements where each chunk is sliced before the found pattern
  • Creates an enumerator for each chunked elements. The beginnings of chunks are defined by pattern and the block. If pattern === elt returns true or the block returns true for the element, the element is beginning of a chunk.
  • Creates an enumerator for each chunked elements. The beginnings of chunks are defined by pattern and the block. If pattern === elt returns true or the block returns true for the element, the element is beginning of a chunk.
partition { |x| ... }
Returns two arrays, the first containing the elements which the block evaluates to true, the second containing the rest.
group_by { |x| ... }
Returns a hash where the keys are the evaluated result from the block and the values are arrays of elements in the collection that correspond to the key.

Other

  • collect { |x| ... }
  • map { |x| ... }
Creates a new array containing the values returned by the block.
  • collect_concat { |x| ... }
  • flat_map { |x| ... }
Returns a new array made by concatenating all block results. Usually equivalent to map { |x| ... }.flatten
  • inject(sym)
  • inject(init, sym)
  • inject { |memo, obj| ... }
  • inject(init) { |memo, obj| ... }
  • reduce ...

Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.

If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element. If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.

If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo.

    each_with_object(o) {|x, o| ... }
Iterates the given block for each element with an arbitrary object given, and returns the initially given object.
  • zip(arg, ...)
  • zip(arg, ...) { |arr| ... }

Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument.

This generates a sequence of ary.size n-element arrays, where n is one more than the count of arguments.

If the size of any argument is less than the size of the initial array, nil values are supplied.

If a block is given, it is invoked for each output array, otherwise an array of arrays is returned.