2

So I have an array which shows which levels are allowed to do things.

allowed = ["user", "admin"]

There's another array that shows which groups a user belongs to.

groups = ["user", "crazy"]

What's the best way to search the allowed array for ANY of the groups a user belongs to? I know it's easy but I'm drawing a real blank here...

2 Answers 2

9

Just &:

allowed & groups


Sign up to request clarification or add additional context in comments.

3 Comments

Way better than my answer. +1
oh my gosh...let's all pretend this never happened ok ... ;-) Thanks!
it has a limit on when you can approve answers...had to wait 5 minutes. Thanks for everyone's help!
0

Convert to a set and do an intersection.

require 'set'

allowed = ["user", "admin"]
has = ["user", "print"]
puts(allowed.to_set.intersection(has.to_set)) # prints #<Set: {"user"}>

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.