---
title: "Ruby 2.5 enumerable predicates accept pattern argument"
description:
  "all?, any?, one? and none? method accept a single pattern argument and
  perform case equality with that argument and it's contents"
canonical_url: "https://www.bigbinary.com/blog/ruby-2-5-enumerable-predicates-accept-pattern-argument"
markdown_url: "https://www.bigbinary.com/blog/ruby-2-5-enumerable-predicates-accept-pattern-argument.md"
---

# Ruby 2.5 enumerable predicates accept pattern argument

all?, any?, one? and none? method accept a single pattern argument and perform
case equality with that argument and it's contents

- Author: Prathamesh Sonpatki
- Published: January 2, 2018
- Categories: Ruby 2.5, Ruby

Ruby 2.5.0 was recently
[released](https://www.ruby-lang.org/en/news/2017/12/25/ruby-2-5-0-released/).

Ruby has sequence predicates such as `all?`, `none?`, `one?` and `any?` which
take a block and evaluate that by passing every element of the sequence to it.

```ruby
if queries.any? { |sql| /LEFT OUTER JOIN/i =~ sql }
logger.log "Left outer join detected"
end
```

Ruby 2.5 allows using a shorthand for this by
[passing a pattern argument](https://bugs.ruby-lang.org/issues/11286).
Internally `case equality operator(===)` is used against every element of the
sequence and the pattern argument.

```ruby
if queries.any?(/LEFT OUTER JOIN/i)
logger.log "Left outer join detected"
end

# Translates to:

queries.any? { |sql| /LEFT OUTER JOIN/i === sql }

```

This allows us to write concise and shorthand expressions where block is only
used for comparisons. This feature is applicable to `all?`, `none?`, `one?` and
`any?` methods.

### Similarities with Enumerable#grep

This feature is based on how `Enumerable#grep` works. `grep` returns an array of
every element in the sequence for which the `case equality operator(===)`
returns true by applying the pattern. In this case, the `all?` and friends
return true or false.

There is a [proposal](https://bugs.ruby-lang.org/issues/14197) to add it for
`select` and `reject` as well.

## Links

- [Human page](https://www.bigbinary.com/blog/ruby-2-5-enumerable-predicates-accept-pattern-argument)
