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
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.
- 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.
- 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
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.
- 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.
- Array#combination
Generate unordered groups from arrays with Array#combination in Ruby. Creates distinct sets for team building, lottery picks, and combinatorial testing.
- 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.
- 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, making set-style subtraction easy in Ruby.
- Array#drop
Array#drop returns all elements except the first n elements from a collection, which makes it useful for paging and skipping headers.
- 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.
- 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 chunks with each_slice and sliding windows with each_cons in Ruby. Covers pagination, batch processing, and running average calculations.
- 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.
- 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.
- 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.
- 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.
- 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
Collapse nested arrays into a flat list with Array#flatten in Ruby. Control depth with an integer argument for partial or full recursive flattening.
- 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.
- Array#flatten!
Flatten nested arrays in place with Ruby's `flatten!`. Collapses multi-dimensional structures recursively, controls depth, returns nil on no change.
- Array#include?
Check if a Ruby array contains a value with Array#include? — syntax, equality semantics, performance, and gotchas.
- 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.
- 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.
- 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.
- 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.
- 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
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.
- 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, returning a new array with each block result so you can reshape data in a clear, predictable way.
- 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.
- Array#max_by
Array#max_by returns the element with the largest block value, or an Array of the top n. Inherited from Enumerable.
- 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.
- 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#pack
Format an array's elements into a binary string using a template of single-letter directives for integers, floats, and strings.
- Array#permutation
Generate ordered arrangements of array elements with Array#permutation in Ruby. Useful for scheduling problems, test case generation, and combinatorial search.
- 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
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.
- 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.
- 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.
- 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.
- Array#rassoc
Search an array of arrays by second element and return the first matching sub-array. The counterpart to Array#assoc.
- Array#reduce
Use array reduce to combine elements with a binary operation, accumulating a running result. Master the idiomatic Ruby fold with practical examples.
- 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#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.
- Array#repeated_permutation
Array#repeated_permutation yields every ordered length-n tuple with replacement, or returns an Enumerator. Covers the size formula and gotchas.
- 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.
- 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
Array#rotate returns a new array with elements shifted by a given count, and rotate! updates the original array in place.
- 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 from a Ruby array with .sample or shuffle the entire array with .shuffle. Covers reproducible random seeds and edge case handling.
- 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
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.
- Array#shuffle
Use Array#shuffle to return a new array of elements in random order. Signature, behavior, and the random: keyword.
- 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.
- 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.
- Array#sort
Sort array elements by value or custom criteria in Ruby when you need predictable ordering for display, search, and follow-up processing.
- 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.
- 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, stopping at the first falsy result. Learn Array#take_while with examples.
- Array#tally
Count occurrences of each element in an array. Returns a hash where keys are elements and values are their counts.
- 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.
- 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.
- 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` and keep the first occurrence of each value in order.
- 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.
- 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.
- 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.