rubyguides

Reference

Array Methods

Methods available on Ruby Array objects.

  1. Array#all?

    Checks if all elements in an array match a condition. Returns true if every element satisfies the block or is truthy.

  2. Array#any?

    Checks if any element in an array matches a condition. Returns true if at least one element satisfies the block or is truthy.

  3. Array#append

    Array#append adds elements to the end of a Ruby array in place, returning the same object for chaining. Amortized O(1) and avoids intermediate copies.

  4. Array#assoc

    Ruby's assoc searches an array of arrays for the first sub-array whose first element matches a key. Use it for simple key-value table lookups without a hash.

  5. Array#bsearch

    Binary search for elements in a sorted array. Supports find-minimum and find-any modes for efficient O(log n) lookups.

  6. Array#bsearch

    Use Array#bsearch for fast binary search on sorted arrays in Ruby. Find elements in O(log n) time with find-minimum and find-any modes for precise lookups.

  7. Array#chunk_while

    Use Ruby's array chunk_while to group adjacent elements into variable-size chunks. The block returns true to keep neighbors together, false to split.

  8. Array#combination

    Generate unordered groups from arrays with Array#combination in Ruby. Creates distinct sets for team building, lottery picks, and combinatorial testing.

  9. Array#compact

    Array#compact strips nil values from arrays, returning a clean copy. Covers the non-destructive form, the in-place variant, and common cleanup patterns.

  10. Array#delete

    Remove elements from arrays by value in Ruby. delete removes all matching elements and returns the removed value.

  11. Array#delete_at

    Remove elements from arrays by index in Ruby. delete_at removes the element at a specific position and returns it.

  12. Array#detect

    Returns the first element in an array or enumerable that matches a condition, or nil if nothing matches. Alias for find.

  13. Array#difference

    Returns a new array containing elements that are not present in the given arrays, making set-style subtraction easy in Ruby.

  14. Array#drop

    Array#drop returns all elements except the first n elements from a collection, which makes it useful for paging and skipping headers.

  15. Array#drop_while

    Drop elements from the front of an array while a condition holds with Array#drop_while in Ruby. Keeps the rest intact for sorted data and log processing.

  16. Array#each_index

    Learn how Ruby Array#each_index yields positions instead of elements, when to use it, and how it compares with each_with_index.

  17. Array#each_slice

    Split arrays into chunks with each_slice and sliding windows with each_cons in Ruby. Covers pagination, batch processing, and running average calculations.

  18. Array#each_with_object

    Build a hash, array, or other mutable accumulator while iterating an array. The memo you pass in is returned intact at the end.

  19. Array#filter_map: Filter and Map in One Pass

    Returns a new array with the truthy results of running a block over every element. A one-pass replacement for `select` + `map` chains. Added in Ruby 2.7.

  20. Array#find

    Array#find returns the first element matching a block condition, or nil when nothing matches. Covers search patterns, hash lookups, and safe navigation.

  21. Array#first

    Returns the first element of an array, or the first n elements as a new array for quick previews, queues, and ordered data.

  22. Array#flatten

    Recursively flattens a nested array into a single array. Use flatten(level) to limit depth, or flatten! to mutate in place.

  23. Array#flatten

    Collapse nested arrays into a flat list with Array#flatten in Ruby. Control depth with an integer argument for partial or full recursive flattening.

  24. Array#flatten_map

    Combine map and flatten(1) into one method with Array#flat_map, transforming each element and flattening the result by one level.

  25. Array#flatten!

    Flatten nested arrays in place with Ruby's `flatten!`. Collapses multi-dimensional structures recursively, controls depth, returns nil on no change.

  26. Array#include?

    Check if a Ruby array contains a value with Array#include? — syntax, equality semantics, performance, and gotchas.

  27. Array#index

    Array#index returns the zero-based index of the first matching element in a Ruby array, scanned by value or block predicate, or nil when no match is found.

  28. Array#inject

    Ruby's Array#inject method combines all array elements by applying a binary operation, accumulating a running result. Also known as reduce in other languages.

  29. Array#insert

    Insert objects into a Ruby Array at a given index. Mutates and returns self; supports negative indices and nil-padded out-of-range inserts.

  30. Array#intersection

    Returns a new array containing the elements common to all given arrays, while preserving duplicates from the first array and keeping first-array order.

  31. Array#intersection

    Returns a new array containing elements common to all arrays, removing duplicates. The & operator provides the same functionality.

  32. Array#join

    Joins array elements into a string separated by a given delimiter. Converts each element to a string recursively for nested arrays.

  33. Array#last

    Ruby's Array#last returns the final element or the last n elements of an array. Use last for stack tops, recent entries, and sorted data extremes.

  34. Array#map

    Returns a new array with the results of running a block on every element. Also available as collect, an exact synonym, and map! for in-place mutation.

  35. Array#map

    Transform array elements with map and collect methods in Ruby, returning a new array with each block result so you can reshape data in a clear, predictable way.

  36. Array#max

    Ruby's Array#max, min, and minmax find the largest and smallest elements in arrays. Use max(n) for top n values and minmax for both extremes in one pass.

  37. Array#max_by

    Array#max_by returns the element with the largest block value, or an Array of the top n. Inherited from Enumerable.

  38. Array#min_by: pick the smallest element by a derived key

    Ruby's array min_by method picks the element with the smallest derived key, with the n-argument and Enumerator forms covered.

  39. Array#none?

    Checks if no elements in an array match a condition. Returns true if no element satisfies the block or is truthy.

  40. Array#one?

    Checks if exactly one element in an array matches a condition. Returns true if precisely one element satisfies the block or is truthy.

  41. Array#pack

    Format an array's elements into a binary string using a template of single-letter directives for integers, floats, and strings.

  42. Array#permutation

    Generate ordered arrangements of array elements with Array#permutation in Ruby. Useful for scheduling problems, test case generation, and combinatorial search.

  43. Array#pop

    Removes and returns the last element of an array. Mutates the array in place. With an argument, removes and returns the last n elements as a new array.

  44. Array#pop

    Array#pop removes and returns elements from the end of a Ruby array. Use it with push, shift, and unshift to model stacks and queues.

  45. Array#prepend

    Add elements to the front of a Ruby array in place with prepend, which mutates the receiver and returns the same array for chaining and queue-style workflows.

  46. Array#product

    Compute all possible combinations across arrays with Array#product in Ruby. Ideal for building test matrices, grids, and option lists from multiple input sets.

  47. Array#push

    Use Array#push in Ruby to append one or more elements to the end of an array, mutating the array in place and returning self for chaining or queue operations.

  48. Array#rassoc

    Search an array of arrays by second element and return the first matching sub-array. The counterpart to Array#assoc.

  49. Array#reduce

    Use array reduce to combine elements with a binary operation, accumulating a running result. Master the idiomatic Ruby fold with practical examples.

  50. Array#reject

    Filter out elements from an array based on a condition. Returns a new array excluding elements for which the block returns true.

  51. Array#repeated_combination: Multiset Combinations in Ruby

    Array#repeated_combination enumerates multiset combinations in Ruby. Includes the block form, the lazy Enumerator, the counting formula, and edge cases.

  52. Array#repeated_permutation

    Array#repeated_permutation yields every ordered length-n tuple with replacement, or returns an Enumerator. Covers the size formula and gotchas.

  53. Array#reverse

    Reverse the order of elements in a Ruby array. Returns a new list with items in reverse order, or use reverse! to modify the original collection in place.

  54. Array#rotate

    Returns a new array with elements rotated so the element at the given offset becomes the first element. The bang variant rotates in place. O(n) time complexity.

  55. Array#rotate

    Array#rotate returns a new array with elements shifted by a given count, and rotate! updates the original array in place.

  56. Array#sample

    Returns one or more random elements from an array. With an argument n, returns an array of up to n unique elements chosen at random.

  57. Array#sample

    Randomly select elements from a Ruby array with .sample or shuffle the entire array with .shuffle. Covers reproducible random seeds and edge case handling.

  58. Array#select

    Returns a new array containing all elements for which the block evaluates to true. The bang variant select! mutates the original array in place.

  59. Array#select

    Filter array elements based on a condition. Returns a new array containing only elements for which the block returns true.

  60. Array#shift

    Array#shift removes and returns the first element of a Ruby array, or the first n elements as an array. It is O(n) because later elements move down.

  61. Array#shuffle

    Use Array#shuffle to return a new array of elements in random order. Signature, behavior, and the random: keyword.

  62. Array#size

    Ruby's size method returns element counts for arrays, hashes, strings, and more. Use size for quick length checks, loop bounds, and conditional logic.

  63. Array#slice_when

    Array#slice_when splits an array into slices between adjacent elements when the block returns true. Useful for runs, sign changes, and pair-based grouping.

  64. Array#sort

    Sort array elements by value or custom criteria in Ruby when you need predictable ordering for display, search, and follow-up processing.

  65. Array#sort_by

    Sort an array by a derived key, using a Schwartzian Transform. Returns a new array; use the bang form to sort in place.

  66. Array#sum

    Sum array elements with the .sum method in Ruby. Returns the sum of all elements, with optional initial value.

  67. Array#take

    Extract or skip elements from the beginning of an array. take returns first n elements, drop skips first n, and the _while variants use a block condition.

  68. Array#take_while

    Returns elements from the beginning of an array while the block returns truthy, stopping at the first falsy result. Learn Array#take_while with examples.

  69. Array#tally

    Count occurrences of each element in an array. Returns a hash where keys are elements and values are their counts.

  70. Array#tally

    Array tally counts occurrences of each element and returns a hash of frequency pairs. Build a frequency table from any Ruby collection in one call.

  71. Array#union

    Array#union merges arrays into one, removing duplicates and preserving first-occurrence order. Covers the | operator, multi-array union, and common use cases.

  72. Array#uniq

    Returns a new array with duplicate elements removed. Supports a block for custom uniqueness logic. The bang variant uniq! mutates the array in place.

  73. Array#uniq

    Remove duplicate elements from arrays in Ruby with `uniq` and keep the first occurrence of each value in order.

  74. Array#unshift

    Prepend one or more elements to the front of an array with Array#unshift. Returns the receiver, mutates in place. Alias: Array#prepend.

  75. Array#zip

    Combines two or more arrays element-by-element into a collection of tuples using zip in Ruby. Pairs corresponding elements, filling shorter arrays with nil.

  76. Array#zip

    Combine each array element with matching values from other arrays using Array#zip to produce grouped rows that are easier to read and pass around.