Array Methods
Methods available on Ruby Array objects.
Array#all?
Checks if all elements in an array match a condition. Returns true if every element satisfies the block or is truthy.
arr.all? { |element| block } -> true or false 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.
arr.any? { |element| block } -> true or false Array#append
Adds one or more elements to the end of an array, mutating it in place. Returns the same array object.
arr.append(*objects) -> arr Array#assoc
Searches an array of arrays for the first sub-array with a matching first element.
array.assoc(key) -> sub_array or nil 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.
arr.bsearch { |x| block } -> obj or nil Array#combination
Generate all combinations of array elements. combination selects unordered arrangements of a given size.
arr.combination(n) { |c| block } 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.
arr.compact -> array Array#delete
Remove elements from arrays by value in Ruby. delete removes all matching elements and returns the removed value.
arr.delete(obj) -> obj or nil Array#delete_at
Remove elements from arrays by index in Ruby. delete_at removes the element at a specific position and returns it.
arr.delete_at(index) -> obj or nil Array#detect
Returns the first element in an array or enumerable that matches a condition, or nil if nothing matches. Alias for find.
arr.detect { |element| block } -> obj or nil 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.
arr.drop(n) -> array Array#drop_while
Skips elements from the beginning of an array while the block returns truthy.
arr.drop_while { |element| block } -> array 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.
arr.find { |element| block } -> obj or nil Array#first
Returns the first element of an array, or the first n elements as a new array.
arr.first(n=nil) -> obj or 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.
arr.flatten(depth=nil) -> 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.flatten!(level=nil) -> array or nil Array#inject
Combines all elements by applying a binary operation, accumulating a running result. Also known as reduce in other languages.
inject(initial = nil) { |memo, element| block } -> result 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 & other_array -> array Array#join
Joins array elements into a string separated by a given delimiter. Converts each element to a string recursively for nested arrays.
arr.join(separator=nil) -> string Array#last
Returns the last element of an array, or the last n elements as a new array.
arr.last(n=nil) -> obj or 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.
arr.none? { |element| block } -> true or false 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.
arr.one? { |element| block } -> true or false Array#permutation
Generate all permutations of array elements. permutation selects ordered arrangements of a given size.
arr.permutation(n) { |p| block } 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.
arr.pop(n=nil) -> obj or array Array#prepend
Adds one or more elements to the beginning of an array, mutating it in place. Returns the same array object.
arr.prepend(*objects) -> arr 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.
reduce(initial = nil) { |memo, element| block } -> result 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.
arr.reject { |element| block } -> new_array 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.
arr.select { |element| block } -> new_array 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.
collection.size -> integer 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.
arr.take(n) -> array Array#take_while
Returns elements from the beginning of an array while the block returns truthy.
arr.take_while { |element| block } -> array 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.