Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic ruby array code: Testing if an array has a specific integer

I'm trying to test if an array has a specific integer in it. Right now I'm using this test;

 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'd prefer to just test for the integer "2" rather than explicitly write out every possible combination of numbers containing "2". If you have any ruby advice I would really appreciate it. This is the best I could imagine on the fly.

Thanks!

like image 295
JZ. Avatar asked Dec 17 '22 04:12

JZ.


2 Answers

Are you looking for Array#include??

current_user.role_ids.include?(2)
like image 99
Ciarán Walsh Avatar answered May 21 '23 15:05

Ciarán Walsh


a = [1,2,3,4,5]
a.include?(2)
like image 40
Ben Avatar answered May 21 '23 14:05

Ben