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

    Adds one or more elements to the end of an array, mutating it in place. Returns the same array object.

  4. Array#assoc

    Searches an array of arrays for the first sub-array with a matching first element.

  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

    Binary search for elements in sorted arrays using .bsearch in Ruby. Find elements in O(log n) time.

  7. Array#combination

    Generate all combinations of array elements. combination selects unordered arrangements of a given size.

  8. Array#compact

    Removes all nil values from an array. The bang variant compact! mutates the original array in place and returns nil if no changes were made.

  9. Array#compact

    Removes nil values from an array, returning a new array. The destructive variant modifies the array in place.

  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.

  14. Array#drop

    Returns all elements except the first n elements from an array.

  15. Array#drop_while

    Skips elements from the beginning of an array while the block returns truthy.

  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 consecutive or sliding windows with each_slice and each_cons

  18. Array#find

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

  19. Array#first

    Returns the first element of an array, or the first n elements as a new array.

  20. Array#flatten

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

  21. Array#flatten

    Returns a new array with all nested arrays recursively collapsed into a single flat array.

  22. Array#flatten_map

    Combines map and flatten(1) into one method. Transforms each element and flattens the result by one level.

  23. Array#flatten!

    Flattens a nested array in place, converting multi-dimensional arrays to single-dimensional.

  24. Array#inject

    Combines all elements by applying a binary operation, accumulating a running result. Also known as reduce in other languages.

  25. Array#intersection

    Returns a new array containing elements common to all arrays, preserving duplicates from the first array.

  26. Array#intersection

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

  27. Array#join

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

  28. Array#last

    Returns the last element of an array, or the last n elements as a new array.

  29. 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.

  30. Array#map

    Transform array elements with map and collect methods in Ruby. Returns a new array with the results of running the block once for each element.

  31. Array#max

    Find maximum and minimum elements in Ruby arrays using .max, .min, and .minmax methods.

  32. Array#none?

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

  33. 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.

  34. Array#permutation

    Generate all permutations of array elements. permutation selects ordered arrangements of a given size.

  35. 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.

  36. Array#pop

    Add and remove elements from array ends. pop/shift remove, push/unshift add.

  37. Array#prepend

    Adds one or more elements to the beginning of an array, mutating it in place. Returns the same array object.

  38. Array#product

    Generate the cartesian product of arrays in Ruby. Creates all possible combinations from multiple arrays.

  39. Array#push

    Appends one or more elements to the end of an array. Mutates the array in place and returns self.

  40. Array#reduce

    Combines all elements by applying a binary operation, accumulating a running result. Alias for inject.

  41. Array#reject

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

  42. Array#reject

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

  43. Array#reverse

    Returns a new array with elements in reverse order. Available in Ruby 1.8+.

  44. 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.

  45. Array#rotate

    Rotates array elements in place, returning a new array with elements shifted. Available in Ruby 1.9.2+.

  46. 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.

  47. Array#sample

    Randomly select elements with .sample or shuffle array order with .shuffle in Ruby.

  48. 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.

  49. Array#select

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

  50. Array#shift

    Removes and returns the first element of an array, or the first n elements as an array. O(n) since all subsequent elements must be shifted down by one position.

  51. Array#size

    Returns the number of elements in a collection (strings, arrays, hashes) or characters in a string.

  52. Array#sort

    Sort array elements by value or custom criteria in Ruby

  53. Array#sum

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

  54. 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.

  55. Array#take_while

    Returns elements from the beginning of an array while the block returns truthy.

  56. Array#tally

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

  57. Array#tally

    Counts the occurrence of each element in an array and returns a hash.

  58. Array#union

    Returns a new array containing all elements from self and given arrays, duplicates removed. Added in Ruby 3.1.

  59. 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.

  60. Array#uniq

    Remove duplicate elements from arrays in Ruby with uniq method. Returns a new array with unique elements.

  61. Array#zip

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

  62. Array#zip

    Combines each element from self with corresponding elements from each given array.