# Array Inquiry in Rails

*Published on January 08, 2023*

---


You can check if an array contains a particular element using the&nbsp;`include?(obj)`&nbsp;method.

```ruby
browsers = ['chrome', 'safari', 'firefox']

browsers.include?('chrome')   # true
browsers.include?('explorer') # false
```

This code is already very simple and readable. But, can we go one step further and call a method named&nbsp;`chrome?`&nbsp;on the array itself?

```ruby
browsers = ['chrome', 'safari', 'firefox']

browsers.chrome?   # should return true
browsers.explorer? # should return false
```

With a little bit of metaprogramming in Ruby, we absolutely can.

## Extending Array

Let's create a new class that extends Ruby's&nbsp;`Array`&nbsp;class and override the&nbsp;[`method_missing`](https://ruby-doc.org/core-3.1.1/BasicObject.html#method-i-method_missing)&nbsp;method on it. This method is defined on the&nbsp;`BasicObject`&nbsp;class, which all Ruby classes inherit from, and the Ruby interpreter calls it when it can't find the method on an object.

```ruby
class ArrayChecker < Array
  def method_missing(name, *args)
    if name.end_with?("?")
      any?(name[0..-2])
    else
      super
    end
  end
end
```

By default, the interpreter raises an error when this method is called. However, you can override this method to provide more dynamic behavior and handle undefined methods.

```ruby
class Person
  def method_missing(symbol, *args)
    puts "You called '#{symbol}' method which doesn't exist."
  end
end

ak = Person.new
ak.name  # You called 'name' method which doesn't exist.
```

The&nbsp;`ArrayChecker`&nbsp;class overrides the&nbsp;`method_missing`&nbsp;method and checks if the method that was called ends with&nbsp;`?`. If it does, then it calls the&nbsp;[`any?`](https://ruby-doc.org/core-3.1.1/Array.html#method-i-any-3F)&nbsp;method on the array, passing the method name without the&nbsp;`?`. This method returns&nbsp;`true`&nbsp;if the array contains the value passed.

For example, if you call&nbsp;`browsers.chrome?`&nbsp;where&nbsp;`browsers`&nbsp;is an instance of the&nbsp;`ArrayChecker`&nbsp;class, the Ruby interpreter will first check if there's a method named&nbsp;`chrome?`&nbsp;on&nbsp;`ArrayChecker`.

Since&nbsp;`ArrayChecker`&nbsp;doesn't include&nbsp;`chrome?`&nbsp;method, Ruby will call the&nbsp;`method_missing`, which in turn calls&nbsp;`browsers.any?('chrome')`&nbsp;, checking if the array includes a value named 'chrome'. If yes, it returns true, returning false otherwise.

This allows us to write the following code.

```ruby
browsers = [:chrome, :safari, :firefox]

browsers.chrome?    # true
browsers.explorer?  # false
```

## Use ActiveSupport::ArrayInquirer

Luckily, if you're using Ruby on Rails, you don't have to write the above code yourself. The Active Support library provides an&nbsp;[`ArrayInquirer`](https://api.rubyonrails.org/classes/ActiveSupport/ArrayInquirer.html)&nbsp;class which handles it for you. It also supports both string and symbol values.

Wrapping an array in&nbsp;`ArrayInquirer`&nbsp;provides a friendlier way to check its contents:

```ruby
browsers = ActiveSupport::ArrayInquirer.new([:chrome, :firefox])

browsers.chrome?    # => true
browsers.firefox?   # => true
browsers.explorer?  # => false
```

In addition, this class also overrides the&nbsp;`any?`&nbsp;method, so it checks for both the stringified and symbolized form of any element in the array.

```ruby
browsers = ActiveSupport::ArrayInquirer.new([:chrome, :firefox])

browsers.any? # true
browsers.any?(:chrome, :firefox) # true
browsers.any?(:explorer, :firefox) # true
browsers.any?(:explorer, :desktop) # false

browsers.any?('chrome') # true
browsers.any?('explorer') # false
```

You can check the implementation of this class on&nbsp;[Github](https://github.com/rails/rails/blob/8015c2c2cf5c8718449677570f372ceb01318a32/activesupport/lib/active_support/array_inquirer.rb).

## The inquiry Helper

In addition, Active Support opens the&nbsp;`Array`&nbsp;class and adds the&nbsp;`inquiry`&nbsp;method to it. That way, you don't have to write&nbsp;`ActiveSupport::ArrayInquirer.new(array)`&nbsp;every time. Instead, you can simply call&nbsp;`array.inquiry`&nbsp;method to get the same effect.

```ruby
pets = [:cat, :dog].inquiry

pets.cat?     # => true
pets.horse?   # => false

pets.any?(:cat, :horse)  # => true
pets.any?(:horse, :cow)  # => false
```

Here's the implementation of the&nbsp;`Array#inquiry`&nbsp;method. It instantiates the&nbsp;`ArrayInquirer`&nbsp;class, passing&nbsp;`self`&nbsp;which is the array itself.

```ruby
class Array
  def inquiry
    ActiveSupport::ArrayInquirer.new(self)
  end
end
```

So this concludes our exploration of the&nbsp;`ArrayInquirer`&nbsp;class. You can use this class as a readable way to check the contents of an array.

Hope you found this post helpful.

