位在敲打红宝石 [英] Bit banging in ruby
本文介绍了位在敲打红宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个位,将包含安全权限给定用户。
I want to create a bit, that will contain security permissions for a given user.
在C#中,我将通过创建一个枚举做到这一点,然后我会做一些位敲打二进制值,安定'和;&放大器;'看它是否导致了真正的价值。
In c#, I would do this by creating an enumeration, and then I would do some bit banging on the binary value, by anding '&&' to see if it results in a TRUE value.
我怎样才能做到这一点最好的红宝石?
How can I do this best in Ruby?
推荐答案
如果潜在价值是非常重要的,那么你可以创建你使用像一个枚举模块
If the underlying value is important then you can create a module that you use like an enum
module Groups
ADMIN = 1
BOSS = 2
CLERK = 4
MEAT = 8
BREAD = 16
CHEESE = 32
end
要设置权限只是按位或
在一起
To set permissions just bitwise or
them together
permissions = Groups::BOSS | Groups::MEAT | Groups::CHEESE
和测试你做一个按位和
>> permissions & Groups::CHEESE > 0
=> true
>> permissions & Groups::BREAD > 0
=> false
我也喜欢你如何让实际的位掩码更具可读性与 _
像这样
permissions = 0b0010_1010
这篇关于位在敲打红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文