Boosting Performance and Readability: ActiveSupport::ArrayInquirer vs. includes

ActiveSupport::ArrayInquirer is a handy utility class that allows you to easily work with arrays. It makes it easy to check whether an array includes a certain value without having to write complex code.

Let’s explore an example of a real-life scenario where ActiveSupport::ArrayInquirer can be useful if compare it to the traditional approach using includes.

Say you’re building a web application that requires user authentication and authorization. You have different types of users, each with its own set of permissions. You’ve chosen to store these permissions as an array of strings in your database. Suppose you wish to implement a functionality that lets you determine whether a user has permission to access certain features of the application.

One way to implement this feature is to use the includes method provided by Ruby. Here’s an example of how you might do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Given a list of user permissions
user_permissions = ["create", "delete", "read", "publish"]

# Check if the user has the "create" permission
if user_permissions.include?("create")
  # Allow access to create functionality
else
  # Deny access to create functionality
end

# Check if the user has the "delete" permission
if user_permissions.include?("delete")
  # Allow access to delete functionality
else
  # Deny access to delete functionality
end

# Check if the user has both "read" and "publish" permissions
if user_permissions.include?("read") && user_permissions.include?("publish")
  # Allow access to read and publish functionality
else
  # Deny access to read and publish functionality
end

This code works, but it can be difficult to read and understand, especially if you have many different permissions. Additionally, it can be slow if you have a large number of users in your database.

Now, let’s see how you can simplify this code using ActiveSupport::ArrayInquirer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Given a list of user permissions
user_permissions = ["create", "delete", "read", "publish"]

# Create an ArrayInquirer instance with the user permissions
inquirer = ActiveSupport::StringInquirer.new(user_permissions)

# Check if the user has the "create" permission
if inquirer.create?
  # Allow access to create functionality
else
  # Deny access to create functionality
end

# Check if the user has the "delete" permission
if inquirer.delete?
  # Allow access to delete functionality
else
  # Deny access to delete functionality
end

# Check if the user has both "read" and "publish" permissions
if inquirer.read? && inquirer.publish?
  # Allow access to read and publish functionality
else
  # Deny access to read and publish functionality
end

In this code, we create a new ActiveSupport::StringInquirer instance with the user permissions. This allows us to call the method on the inquirer object, which returns true or false.

This code is much easier to read and understand than the previous example.