4

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!

2 Answers 2

9

Are you looking for Array#include??

current_user.role_ids.include?(2)
Sign up to request clarification or add additional context in comments.

2 Comments

Any difference between using include? or member? ? Is one considered slower or less idiomatic?
There’s little effective difference between the two, but include? is more common.
3
a = [1,2,3,4,5]
a.include?(2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.