Array#reject

arr.reject { |element| block } -> new_array
Returns: Array · Updated March 13, 2026 · Array Methods
ruby arrays enumerable filtering

Array#reject does the opposite of select. It filters out elements from an array based on a condition you define in a block, returning a new array containing only the elements for which the block evaluates to falsy.

This is incredibly useful when you want to remove unwanted elements from a collection. Unlike delete, it works based on a condition rather than a specific value.

Syntax

array.reject { |element| block }
array.reject  # without block returns an Enumerator

When you omit the block, reject returns an Enumerator, allowing you to chain other methods or use it with various iteration patterns.

Parameters

ParameterTypeDefaultDescription
blockProcRequiredA block that receives each element. Return true to exclude the element, falsy to keep it.

Return Value

Returns a new Array containing all elements for which the block returned a falsy value (nil or false). The original array remains unchanged. Returns an empty array if all elements are rejected.

Examples

Basic filtering

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Remove even numbers (keep odds)
odds = numbers.reject { |n| n.even? }
# => [1, 3, 5, 7, 9]

# Remove numbers greater than 5
small = numbers.reject { |n| n > 5 }
# => [1, 2, 3, 4, 5]

Working with strings

words = ["apple", "banana", "cherry", "date", "elderberry"]

# Keep only short words (less than 6 characters)
short_words = words.reject { |word| word.length >= 6 }
# => ["apple", "date"]

# Remove words containing "a"
no_a = words.reject { |word| word.include?("a") }
# => ["cherry", "elderberry"]

Using with hashes

users = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 17 },
  { name: "Charlie", age: 30 },
  { name: "Diana", age: 19 }
]

# Keep only adults (age >= 18)
adults = users.reject { |user| user[:age] < 18 }
# => [{:name=>"Alice", :age=>25}, {:name=>"Charlie", :age=>30}]

Using without a block (Enumerator)

numbers = [1, 2, 3, 4, 5]

# Chain with other methods
result = numbers.reject.with_index { |n, i| n + i > 4 }
# => [1, 2]

Edge Cases

  • Empty array returns empty array
  • All elements rejected returns empty array
  • No elements rejected returns a copy of the original array
  • Block always returns true: returns empty array
  • Block always returns false: returns copy of original
[].reject { |n| true }
# => []

[1, 2, 3].reject { |n| false }
# => [1, 2, 3]

[1, 2, 3].reject { |n} true }
# => []

Common Mistakes

Confusing with delete

reject removes based on a condition, not a specific value:

# Wrong - this is for delete
[1, 2, 2, 3].reject(2)  # ArgumentError: wrong number of arguments

# Correct - use delete for specific values
[1, 2, 2, 3].delete(2)  # => 2, returns the deleted value

Forgetting it returns a new array

reject doesn’t modify the original:

numbers = [1, 2, 3, 4, 5]
numbers.reject { |n| n > 2 }
# numbers is still [1, 2, 3, 4, 5]

# If you need to modify in place, use reject!
numbers.reject! { |n| n > 2 }
# numbers is now [1, 2]
  • select — keeps elements where block returns true (opposite of reject)
  • delete — removes elements by value or condition
  • detect — returns first matching element

See Also