---
title: "Rails 5 ArrayInquirer and checking array contents"
description:
  "Rails 5 has wrapped Array in an ArrayInquirer giving us a friendlier way to
  check its contents. This post also explains its usage in request variants."
canonical_url: "https://www.bigbinary.com/blog/rails-5-introduces-active-support-array-inquirer"
markdown_url: "https://www.bigbinary.com/blog/rails-5-introduces-active-support-array-inquirer.md"
---

# Rails 5 ArrayInquirer and checking array contents

Rails 5 has wrapped Array in an ArrayInquirer giving us a friendlier way to
check its contents. This post also explains its usage in request variants.

- Author: Mohit Natoo
- Published: May 27, 2016
- Categories: Rails 5, Rails

Rails 5 [introduces Array Inquirer](https://github.com/rails/rails/pull/18939)
that wraps an array object and provides friendlier methods to check for the
presence of elements that can be either a string or a symbol.

```ruby

pets = ActiveSupport::ArrayInquirer.new([:cat, :dog, 'rabbit'])

> pets.cat?
#=> true

> pets.rabbit?
#=> true

> pets.elephant?
#=> false

```

Array Inquirer also has `any?` method to check for the presence of any of the
passed arguments as elements in the array.

```ruby

pets = ActiveSupport::ArrayInquirer.new([:cat, :dog, 'rabbit'])

> pets.any?(:cat, :dog)
#=> true

> pets.any?('cat', 'dog')
#=> true

> pets.any?(:rabbit, 'elephant')
#=> true

> pets.any?('elephant', :tiger)
#=> false

```

Since `ArrayInquirer` class inherits from `Array` class, its `any?` method
performs same as `any?` method of `Array` class when no arguments are passed.

```ruby

pets = ActiveSupport::ArrayInquirer.new([:cat, :dog, 'rabbit'])

> pets.any?
#=> true

> pets.any? { |pet| pet.to_s == 'dog' }
#=> true

```

## Use inquiry method on array to fetch Array Inquirer version

For any given array we can have its Array Inquirer version by calling `inquiry`
method on it.

```ruby

pets = [:cat, :dog, 'rabbit'].inquiry

> pets.cat?
#=> true

> pets.rabbit?
#=> true

> pets.elephant?
#=> false

```

## Usage of Array Inquirer in Rails code

Rails 5
[makes use of Array Inquirer](https://github.com/georgeclaghorn/rails/blob/c64b99ecc98341d504aced72448bee758f3cfdaf/actionpack/lib/action_dispatch/http/mime_negotiation.rb#L89)
and provides a better way of checking for the presence of given variant.

Before Rails 5 code looked like this.

```ruby

request.variant = :phone

> request.variant
#=> [:phone]

> request.variant.include?(:phone)
#=> true

> request.variant.include?('phone')
#=> false

```

Corresponding Rails 5 version is below.

```ruby

request.variant = :phone

> request.variant.phone?
#=> true

> request.variant.tablet?
#=> false

```

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-introduces-active-support-array-inquirer)
