Rails 5 introduces ArrayInquirer
Rails 5
introduced ArrayInquirer as a new member to Inquirer family which initially consisted of well known StringInquirer.
Just for the record, who haven’t met yet with StringInquirer
. Its owing to StringInquirer
that we are able to call Rails.env.production?
instead of Rails.env == 'production'
. Since Rails.env
returns the value wrappped within StringInquirer
.
ArrayInquirer
overrides method_missing
to respond to all of the predicate methods
called on it, validating that if any of the value in ArrayInquirer
object equals name of the predicate method.
vehicles = ActiveSupport::ArrayInquirer.new([:car, :truck])
vehicles.truck? => true
vehicles.car? => true
vehicles.bike? => false
ArrayInquirer
also overrides any?
method, accepting arguments and returning corresponding boolean
based on if any of the value in arguments is present in ArrayInquirer
object.
vehicles.any?(:car, :bike) => true
vehicles.any?(:bike) => false
For the curious audience out there, put on your diving shoes and jump into rails source code to get a more crisp picture of how ArrayInquirer
actually works.