Skip to content

Instantly share code, notes, and snippets.

@taher435
Created December 17, 2015 11:34
Show Gist options
  • Save taher435/e83fbe580cdf2a25a219 to your computer and use it in GitHub Desktop.
Save taher435/e83fbe580cdf2a25a219 to your computer and use it in GitHub Desktop.
User Roles with bit mask
ROLES = %i(student mentor guardian admin td)
@roles_mask = nil
def set(roles)
@roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.inject(0, :+)
end
def get
ROLES.reject do |r|
((@roles_mask.to_i || 0) & 2**ROLES.index(r)).zero?
end
end
puts "setting roles"
set([:mentor, :admin])
puts @roles_mask.to_s(2)
puts "---"
puts "getting roles"
puts get
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment