Ruby array querying, the friendlier way, :inquiry

Merdan Durdiyev
kode-art
Published in
3 min readJul 25, 2023

Introduction

Good day dear readers, coders, Rubyists, and coding enthusiasts.

I am back with one more article about Ruby arrays. We surely know the regular ways of querying, accessing elements of a Ruby array. But besides that, we have a small cheatsheet that lets us querying the Ruby array a piece of cake.

Regular ways

Let’s remember how we would check inclusion of an item in a Ruby array. We would use “include?” method for that purpose.

# Define an array of companies
companies = [:uber, :amazon, :google, :apple, :shopify]

# Check inclusion of a company in the created array
companies.include? :uber # => true
companies.include? :logitech # => false

There is another way to check if an item is included in an array. And that’s by using “any?” array method.

# Define an array of companies
companies = [:uber, :amazon, :google, :apple, :shopify]

# Check inclusion of a company in the created array
companies.any? { |company| company == :uber } # => true
companies.any? { |company| company == :logitech } # => false

One more way to check it is using ActiveRecord “in?” method in Rails. But this time the focus is on the element. For this, we need to require ‘rails’ gem at the top.

require 'rails'

# Define an array of companies
companies = [:uber, :amazon, :google, :apple, :shopify]

# Check inclusion of a company in the created array
:uber.in? companies # => true
:logitech.in? companies # => false

But, beside all of these methods, there’s a more prettier way to check if an item is included in the array. So, let’s take a look at how we can achieve it.

A Friendlier way

In general, this trick will let us check inclusion of array elements in a friendlier way. But this is applicable to string-like contents.

For this we need to wrap the array in ActiveSupport::ArrayInquirer object.

The “inquiry” method does exactly that. It wraps the array in ActiveSupport::ArrayInquirer object.

# File 'activesupport/lib/active_support/core_ext/array/inquiry.rb', line 16

def inquiry
ActiveSupport::ArrayInquirer.new(self)
end

Here is what is happening behind the scenes.

As we mentioned above, the “Class: ActiveSupport::ArrayInquirer” class just gives a friendlier way to query an element in the subject array.

And what it does behind the walls is, it makes use of “method_missing” method. When it does not find such a method, it passes each element of candidates collection to ArrayInquirer collection. The method returns true if any element from the ArrayInquirer collection is equal to the stringified or symbolized form of any element in the candidates collection. Otherwise it calls the “method_mising” method of a parent object.

Here is the code that checks it:

# File 'activesupport/lib/active_support/array_inquirer.rb', line 40

def method_missing(name, *args)
if name.end_with?("?")
any?(name[0..-2])
else
super
end
end

So, finally, here’s the beauty we can apply to an array and write an attractive Ruby code to check inclusion of an element in an array.

BOOM !!!

# Define an array of companies
companies = [:uber, :amazon, :google, :apple, :shopify].inquiry

# Check inclusion of a company in the created array
companies.uber? # => true
companies.logitech? # => false

Conclusion

So, this is it, friends.

Ruby is more than just a “GEM” stone, and there’s still so much hidden in it we can learn and widen our knowledge base.

I am trying to stick to constantly reading docs and good blogs, among which I recommend Akshay’s Blog, which comes with very helpful blogs very often. And of course, after learning a bit myself, I try to share it with others.

Hope you have learnt at least a bit of something new today, after reading this article.

See you in a new article very soon. Stay safe, stay curious, and keep learning !!!

--

--