rubyguides

Reference

Enumerable

Enumerable mixin methods shared across many collection types.

  1. Enumerable#all?

    Returns true if all elements in the collection satisfy the given condition. Returns true for empty collections.

  2. Enumerable#any?

    Returns true if any element satisfies the given condition. Checks truthiness when no block or argument is given.

  3. Enumerable#chunk

    Group consecutive elements by their block value with chunk in Ruby. Perfect for processing runs, states, or categorized data.

  4. Enumerable#count

    Counts elements in a collection that match a condition or returns the total number of elements.

  5. Enumerable#cycle

    Call the given block for each element n times or forever. Returns nil if the loop has finished without a break.

  6. Enumerable#drop

    Returns all elements after the first n elements. Use drop to skip calibration data, remove headers, or split collections into head and tail.

  7. Enumerable#drop_while

    Skips elements from the start of an enumerator while the condition is true, then returns the rest.

  8. Enumerable#each_cons

    Process consecutive elements in groups with each_cons. Perfect for sliding window algorithms, pairwise comparisons, and sequential analysis.

  9. Enumerable#each_slice

    Iterate over consecutive elements in fixed-size groups with each_slice. Useful for batch processing, pagination, and grouping data into chunks.

  10. Enumerable#each_with_index

    Iterates over each element while tracking its index position in the collection.

  11. Enumerable#each_with_object

    Iterate over collection while building and returning an accumulator object with each_with_object in Ruby.

  12. Enumerable#filter_map

    Filter and transform elements in a single pass with filter_map in Ruby. Combines select and map for efficient processing.

  13. Enumerable#find

    Returns the first element that satisfies a given condition. Also available as detect, which is an alias.

  14. Enumerable#first

    Returns the first element or first n elements from a collection. Returns nil for empty collections when called without an argument.

  15. Enumerable#flat_map

    Transform and flatten collections in one pass with flat_map in Ruby. Combines map and flatten into an efficient operation.

  16. Enumerable#group_by

    Groups collection elements by a criteria and returns a hash with keys as group identifiers.

  17. Enumerable#include?

    Checks if any element equals the given object. Returns true or false.

  18. Enumerable#inject

    Reduces a collection to a single value by applying a binary operation repeatedly. Also known as reduce or fold in other languages.

  19. Enumerable#map

    Returns a new array with the results of running block on every element. Also available as collect, which is an exact synonym.

  20. Enumerable#max

    Returns the largest element or n largest elements from an enumerable collection.

  21. Enumerable#max_by

    Find the maximum element in a collection based on the return value of a block with max_by in Ruby.

  22. Enumerable#min

    Returns the smallest element or n smallest elements from an enumerable collection.

  23. Enumerable#min_by

    Find the minimum element in a collection based on the return value of a block with min_by in Ruby.

  24. Enumerable#none?

    Returns true if no element satisfies the given condition. Checks for falsy values when no block or argument is given.

  25. Enumerable#partition

    Splits a collection into two arrays — elements for which the block returns true, and elements for which it returns false.

  26. Enumerable#reduce

    Reduces a collection to a single value by applying a binary operation repeatedly. Also known as inject or fold in other languages.

  27. Enumerable#reject

    Returns an array of all elements for which the block evaluates to false. The inverse of select — removes elements that match the condition.

  28. Enumerable#select

    Returns an array of all elements for which the block evaluates to true. Also available as find_all, which is an exact alias.

  29. Enumerable#sort_by

    Sort collection elements by criteria you define with sort_by in Ruby.

  30. Enumerable#sum

    Calculates the sum of elements in a collection. Adds all elements together with optional initial value and block transformation.

  31. Enumerable#take

    Returns the first n elements from a collection. Returns all elements if n exceeds the collection size — no error is raised.

  32. Enumerable#take_while

    Returns elements from the start of a collection while the condition is true. Stops as soon as the condition is false and discards the remaining elements.

  33. Enumerable#tally

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

  34. Enumerable#uniq

    Returns a new array containing only the unique elements from a collection, preserving original order, with 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.