Bitfield_struct
bf = [true, true, false, false, true]
# to binary string
bs = bf.map { _1 ? 1 : 0 }.join
p bs # => "11001"
# to Integer
int = bs.to_i(2)
p int # => 25
# Struct
BitField = Struct.new(:cond) {
def and(s) = cond.map { _1 ? 1 : 0 }
.join.to_i(2) & s.to_i(2) == s.to_i(2)
}
bf = BitField.new [true, true, false, false, true]
p bf.and '1000' # => true
p bf.and '1001' # => true
p bf.and '10001' # => true
Example usage of this
BitField = Struct.new(:cond) {
def call(o, s) = cond.map { _1[o] ? 1 : 0 }
.join.to_i(2) & s.to_i(2) == s.to_i(2)
}
rw = BitField.new [
->(x) { File.file? x },
->(x) { File.readable? x },
->(x) { File.writable? x }
]
# output only files (not dirs) that are readable and writable
Dir.glob('*').each {|f|
p f if rw.(f, '111')
}