ActiveSupport::ArrayInquirer

Prateek vyas
Jan 16, 2022

--

ActiveSupport::ArrayInquirer

What would you do if you want to check if array consists of particular values?

This must be the first solution in your mind

[:sym1, :sym2].include?(:sym1)
#=> true

But what if i put all the elements as strings?

["str1", "str2"]

Now you have to check it with string..like “str1" or “str2”

something like this

["str1", "str2"].include?("str1")

So you don't have a feasible solution!

That’s where ActiveSupport::ArrayInquirer comes into the action.

You just need to create the array object of ActiveSupport::ArrayInquirer

ar = ActiveSupport::ArrayInquirer.new(["str", :sym, "str2"])ar.str? #=> true
ar.sym? #=> true
ar.abc? #=> false

We also have any? method for this object, which doesn’t care about symbols or strings! and gives true if any of the values match!

ar.any?(:str) #=> true
ar.any?("sym") #=> true

Hope this blog post added a value in your knowledge about ActiveSupport

Happy coding!

--

--

Prateek vyas
Prateek vyas

Written by Prateek vyas

Hello i am prateek vyas working as a ruby developer

No responses yet