The question is pretty self explanatory. I want to turn something like [true, true, false, true]
into the following bitstring 1101.
I assume I need to use Array.pack, but I can't figure out exactly how to do this.
The question is pretty self explanatory. I want to turn something like [true, true, false, true]
into the following bitstring 1101.
I assume I need to use Array.pack, but I can't figure out exactly how to do this.
You can define a
to_i
method forTrueClass
andFalseClass
. Something like this:It really depends on how exactly you want to represent the boolean Array.
For
[true, true, false, true]
, should it be0b1101
or0b11010000
? I assume it's the former, and you may get it like this:PS. There may be further endian problem depending on your requirements.
You can use
map
andjoin
:You can indeed do this with pack
First turn your booleans into a string
Pad the string with zeroes if needed
Then pack