The base code of the ruby ​​array: Testing if the array has a specific integer

I am trying to check if an array has a specific integer. I am using this test now;

 def admin?
    current_user.role_ids == [1,2] || current_user.role_ids == [2] || current_user.role_ids == [1,2,5]
  end

The code works, but I would rather just check the integer "2" rather than explicitly write out every possible combination of numbers containing "2". If you have ruby ​​advice, I would really appreciate it. This is the best I could imagine on the fly.

Thank!

+3
source share
2 answers

Are you looking for Array#include??

current_user.role_ids.include?(2)
+6
source
a = [1,2,3,4,5]
a.include?(2)
+2
source

Source: https://habr.com/ru/post/1726925/


All Articles