rubyguides

Reference

Enumerable

Enumerable mixin methods shared across many collection types.

  1. Enumerable#all?

    Use Enumerable#all? to check if every element in a Ruby collection passes a test. Covers blocks, patterns, short-circuit eval, and empty-collection quirks.

  2. Enumerable#any?

    Enumerable#any? returns true when at least one element satisfies a condition, making it a fast yes-or-no check for Ruby collections.

  3. Enumerable#chunk

    Enumerable#chunk groups consecutive Ruby elements by the value returned from a block. It is useful for runs, states, and categorized data.

  4. Enumerable#count

    Enumerable#count returns the number of elements in a Ruby collection. It can count everything, matching values, or block results.

  5. Enumerable#cycle

    Enumerable#cycle repeats each element n times or forever, returning nil when the loop finishes without a break.

  6. Enumerable#drop

    Enumerable#drop returns all elements after the first n items. Use it to skip calibration data, remove headers, or split a collection into head and tail.

  7. Enumerable#drop_while

    Enumerable#drop_while skips leading elements while a condition holds, returning the rest as an array. Strip prefixes from Ruby sequences with a clear predicate.

  8. Enumerable#each_cons

    Enumerable#each_cons processes consecutive elements in groups, which is useful for sliding windows, pairwise comparisons, and sequential analysis.

  9. Enumerable#each_slice

    Enumerable#each_slice iterates over consecutive elements in fixed-size groups. An essential Ruby enumerable for batch processing, pagination, and chunking data.

  10. Enumerable#each_with_index

    Ruby's Enumerable#each_with_index iterates elements with their index, useful for numbered lists, pairing parallel arrays, and position-based lookups.

  11. Enumerable#each_with_object

    Ruby's Enumerable#each_with_object iterates collections while building a mutable accumulator like a hash or array, returned intact after the loop.

  12. Enumerable#filter_map

    Combine select and map into one efficient pass with Enumerable#filter_map. Filter and transform enumerable elements without building intermediate arrays.

  13. Enumerable#find

    Enumerable#find returns the first element in a collection that matches a given condition, short-circuiting iteration as soon as a match is found in Ruby.

  14. Enumerable#first

    Enumerable#first gets the first element or first n elements from any collection with the Enumerable mixin. Returns nil on empty, [] when passed n.

  15. Enumerable#flat_map

    Enumerable#flat_map transforms each element and flattens the result by one level. It combines map and flatten in a single Ruby pass.

  16. Enumerable#group_by

    Enumerable#group_by organizes Ruby collections into buckets by a shared trait, returning a hash keyed by group identifier with matching element arrays.

  17. Enumerable#include?

    Enumerable#include? checks whether a Ruby collection contains a matching element. It returns true or false and uses == for comparisons.

  18. Enumerable#inject

    Accumulate collections into single values with Ruby's Enumerable#inject. Covers summing, max finding, string building, flattening, and the reduce alias.

  19. Enumerable#map

    Transform collections with Ruby's Enumerable#map, returning a new array of block results. Covers the collect alias, chaining, lazy evaluation, and hash mapping.

  20. Enumerable#max

    Returns the largest element or the n largest elements from an enumerable collection using natural order or a custom comparison block.

  21. Enumerable#max_by

    Use max_by in Ruby to find the maximum element in any enumerable by custom comparison logic. Rank by derived values, distance, or any scoring function.

  22. Enumerable#min

    Ruby's Enumerable#min returns the smallest element or n smallest elements from a collection, supporting custom comparison blocks for flexible ordering.

  23. Enumerable#min_by

    Enumerable#min_by finds the minimum element in a collection using a block. Covers ranking by computed values, finding multiple minima, and practical examples.

  24. Enumerable#none?

    Use Enumerable#none? to check if no element satisfies a condition in a Ruby collection, returning a boolean after testing a block, pattern, or truthiness.

  25. Enumerable#partition

    Use the Enumerable partition method to split a Ruby collection into two arrays in one pass — one for truthy block results, one for falsy.

  26. Enumerable#reduce

    Collapse collections into single values with Ruby's Enumerable#reduce. Covers summing, maxima, building strings, flattening arrays, and the inject alias.

  27. Enumerable#reject

    Filter collections with Ruby's Enumerable#reject, the inverse of select. Removes matching elements. Examples for arrays, hashes, and method chaining.

  28. Enumerable#select

    Enumerable#select returns all elements for which a block evaluates to true, making it the core enumerable method for filtering Ruby collections by condition.

  29. Enumerable#sort_by

    Sort enumerable collection elements by criteria you define with sort_by in Ruby, which is useful for custom ordering and natural comparison rules.

  30. Enumerable#sum

    Enumerable#sum calculates totals for numbers, strings, hashes, and ranges with an optional initial value or block.

  31. Enumerable#take

    Enumerable#take returns the first n elements from any Ruby collection. Use it for previewing data, limiting result sets, and splitting head from tail.

  32. Enumerable#take_while

    Returns elements from the start of an enumerable while the condition is true, then stops as soon as the condition is false and discards the remaining elements.

  33. Enumerable#tally

    Counts occurrences of each element in an enumerable collection in Ruby, returning a hash where keys are elements and values are their counts.

  34. Enumerable#uniq

    Enumerable#uniq returns a new array of unique elements from a collection, preserving insertion order with an optional block for custom uniqueness logic.

  35. Enumerable#zip

    Merge multiple enumerables element-by-element with zip in Ruby. Creates arrays of corresponding elements, filling shorter ones with nil.