Created
July 16, 2022 21:50
-
-
Save jasonkarns/6bddcad252a4168124f53f3b77bc8995 to your computer and use it in GitHub Desktop.
BitInquirer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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