Checks if all elements in an array match a condition. Returns true if every element satisfies the block or is truthy.
Reference
Array Methods
Methods available on Ruby Array objects.
- Array#all?
- 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.
- Array#append
Adds one or more elements to the end of an array, mutating it in place. Returns the same array object.
- Array#assoc
Searches an array of arrays for the first sub-array with a matching first element.
- Array#bsearch
Binary search for elements in a sorted array. Supports find-minimum and find-any modes for efficient O(log n) lookups.
- Array#bsearch
Binary search for elements in sorted arrays using .bsearch in Ruby. Find elements in O(log n) time.
- Array#combination
Generate all combinations of array elements. combination selects unordered arrangements of a given size.
- 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.
- Array#compact
Removes nil values from an array, returning a new array. The destructive variant modifies the array in place.
- Array#delete
Remove elements from arrays by value in Ruby. delete removes all matching elements and returns the removed value.
- Array#delete_at
Remove elements from arrays by index in Ruby. delete_at removes the element at a specific position and returns it.
- Array#detect
Returns the first element in an array or enumerable that matches a condition, or nil if nothing matches. Alias for find.
- Array#difference
Returns a new array containing elements that are not present in the given arrays.
- Array#drop
Returns all elements except the first n elements from an array.
- Array#drop_while
Skips elements from the beginning of an array while the block returns truthy.
- 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.
- Array#each_slice
Split arrays into consecutive or sliding windows with each_slice and each_cons
- Array#find
Returns the first element in an array or enumerable that matches a condition, or nil if nothing matches.
- Array#first
Returns the first element of an array, or the first n elements as a new array.
- Array#flatten
Recursively flattens a nested array into a single array. Use flatten(level) to limit depth, or flatten! to mutate in place.
- Array#flatten
Returns a new array with all nested arrays recursively collapsed into a single flat array.
- Array#flatten_map
Combines map and flatten(1) into one method. Transforms each element and flattens the result by one level.
- Array#flatten!
Flattens a nested array in place, converting multi-dimensional arrays to single-dimensional.
- Array#inject
Combines all elements by applying a binary operation, accumulating a running result. Also known as reduce in other languages.
- Array#intersection
Returns a new array containing elements common to all arrays, preserving duplicates from the first array.
- Array#intersection
Returns a new array containing elements common to all arrays, removing duplicates. The & operator provides the same functionality.
- Array#join
Joins array elements into a string separated by a given delimiter. Converts each element to a string recursively for nested arrays.
- Array#last
Returns the last element of an array, or the last n elements as a new array.
- 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.
- 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.
- Array#max
Find maximum and minimum elements in Ruby arrays using .max, .min, and .minmax methods.
- Array#none?
Checks if no elements in an array match a condition. Returns true if no element satisfies the block or is truthy.
- 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.
- Array#permutation
Generate all permutations of array elements. permutation selects ordered arrangements of a given size.
- 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.
- Array#pop
Add and remove elements from array ends. pop/shift remove, push/unshift add.
- Array#prepend
Adds one or more elements to the beginning of an array, mutating it in place. Returns the same array object.
- Array#product
Generate the cartesian product of arrays in Ruby. Creates all possible combinations from multiple arrays.
- Array#push
Appends one or more elements to the end of an array. Mutates the array in place and returns self.
- Array#reduce
Combines all elements by applying a binary operation, accumulating a running result. Alias for inject.
- 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.
- Array#reject
Filter out elements from an array based on a condition. Returns a new array excluding elements for which the block returns true.
- Array#reverse
Returns a new array with elements in reverse order. Available in Ruby 1.8+.
- 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.
- Array#rotate
Rotates array elements in place, returning a new array with elements shifted. Available in Ruby 1.9.2+.
- 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.
- Array#sample
Randomly select elements with .sample or shuffle array order with .shuffle in Ruby.
- 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.
- Array#select
Filter array elements based on a condition. Returns a new array containing only elements for which the block returns true.
- 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.
- Array#size
Returns the number of elements in a collection (strings, arrays, hashes) or characters in a string.
- Array#sort
Sort array elements by value or custom criteria in Ruby
- Array#sum
Sum array elements with the .sum method in Ruby. Returns the sum of all elements, with optional initial value.
- 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.
- Array#take_while
Returns elements from the beginning of an array while the block returns truthy.
- Array#tally
Count occurrences of each element in an array. Returns a hash where keys are elements and values are their counts.
- Array#tally
Counts the occurrence of each element in an array and returns a hash.
- Array#union
Returns a new array containing all elements from self and given arrays, duplicates removed. Added in Ruby 3.1.
- 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.
- Array#uniq
Remove duplicate elements from arrays in Ruby with uniq method. Returns a new array with unique elements.
- 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.
- Array#zip
Combines each element from self with corresponding elements from each given array.