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 th开发者_如何学Pythone 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...
Just &
:
allowed & groups
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"}>
精彩评论