Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Ruby Dictionary

  1. Home
  2. Ruby Dictionary
  3. Array.flatten / uniq / compact

Array.flatten / uniq / compact

These methods flatten nested arrays, remove duplicates, and eliminate nil elements, respectively. They are commonly used for data cleaning.

Syntax

# Flattens a nested array into one dimension.
array.flatten       # Removes all levels of nesting.
array.flatten(depth)  # Flattens up to the specified depth.

# Returns a new array with duplicate elements removed.
array.uniq
array.uniq { |element| key }  # Specifies the uniqueness criterion via a block.

# Returns a new array with nil elements removed.
array.compact

Method List

MethodDescription
flattenReturns a new array with all nested arrays fully flattened.
flatten(n)Returns a new array with nesting flattened up to n levels deep.
flatten!Flattens the array in place. Returns nil if no changes were made.
uniqReturns a new array with duplicates removed. The first occurrence of each element is kept.
uniq { |e| }Treats elements with the same block return value as duplicates, keeping only the first occurrence.
uniq!Removes duplicates from the array in place. Returns nil if no changes were made.
compactReturns a new array with all nil elements removed.
compact!Removes nil elements from the array in place. Returns nil if no changes were made.

Sample Code

# flatten: Flattens a nested array.
nested = [1, [2, 3], [4, [5, 6]]]
puts nested.flatten.inspect      # [1, 2, 3, 4, 5, 6]
puts nested.flatten(1).inspect   # [1, 2, 3, 4, [5, 6]] (one level only)

# uniq: Removes duplicate elements.
with_dups = [1, 2, 2, 3, 3, 3, 4]
puts with_dups.uniq.inspect  # [1, 2, 3, 4]

# Specify a uniqueness criterion via a block (case-insensitive comparison).
words = ["apple", "Apple", "banana", "BANANA"]
puts words.uniq { |w| w.downcase }.inspect  # ["apple", "banana"]

# compact: Removes nil elements.
data = [1, nil, 2, nil, 3]
puts data.compact.inspect  # [1, 2, 3]

# Combined example: Cleaning an API response.
raw_data = [[1, 2], [3, nil, 4], [2, 5]]
cleaned = raw_data.flatten.compact.uniq.sort
puts cleaned.inspect  # [1, 2, 3, 4, 5]

Notes

flatten, uniq, and compact are all non-destructive methods — they return a new array without modifying the original. Each has a destructive counterpart with a trailing !.

uniq keeps the first occurrence of each element and removes subsequent duplicates. You can pass a block to define a custom comparison key, making it useful for case-insensitive comparisons or deduplication based on a specific field.

flatten!, uniq!, and compact! return nil if no changes were made. Be careful when using them in method chains. If you need a guaranteed array return value, use the non-destructive versions instead.

For adding and removing array elements, see 'insert / delete / delete_at'. For joining arrays, see 'concat / + / zip'.

If you find any errors or copyright issues, please .