Skip to content

Instantly share code, notes, and snippets.

@jasonkarns
Created July 16, 2022 21:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonkarns/6bddcad252a4168124f53f3b77bc8995 to your computer and use it in GitHub Desktop.
Save jasonkarns/6bddcad252a4168124f53f3b77bc8995 to your computer and use it in GitHub Desktop.
BitInquirer
class BitInquirer
def self.[](mask_map)
Class.new(SimpleDelegator) do
mask_map.each do |name, mask|
define_method name do
allbits?(mask)
end
end
end
end
end
# BitInquirer usage:
Permissions = BitInquirer[
create: 0b1,
read: 0b10,
update: 0b100,
delete: 0b1000
]
me = Permissions.new(6)
me.create? # false
me.read? # true
me.update? # true
me.delete? # false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment