Bitmask represents a set of boolean values as an Integer.
This bitmask gem was extracted from amiel/has_bitmask_attributes
Add this line to your application's Gemfile:
gem 'bitmask'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bitmask
Examples:
masks={:cat=>0b0001,:dog=>0b0010,:fish=>0b0100,}bitmask=Bitmask.new(masks,{:cat=>true})bitmask.to_i# => 1bitmask.get:cat# => truebitmask.get:dog# => falsebitmask.set:dog,truebitmask.get:dog# => truebitmask.to_i# => 3bitmask.to_h# => { :cat => true, :dog => true, :fish => false }bitmask=Bitmask.new(masks,0b101)bitmask.to_h# => { :cat => true, :dog => false, :fish => true }bitmask.to_i# => 5bitmask.to_i.to_s(2)# => "101"Bitmask.new(masks,5) == Bitmask.new(masks,{:cat=>true,:dog=>false,:fish=>true})# => true# Least Significant Bit - LSBbitmask=Bitmask.new(masks,{:cat=>true,:fish=>true})bitmask.lsb# => 1bitmask.lsb.to_s(2)# => "1"Bitmask.new(masks,bitmask.lsb).to_a# => [:cat]# Most Significant Bit - MSBbitmask=Bitmask.new(masks,0b101)bitmask.msb# => 4bitmask.msb.to_s(2)# => "100"Bitmask.new(masks,bitmask.msb).to_a# => [:fish]masks={:cat=>0b0001,:dog=>0b0010,:fish=>0b0100,}bitmask=Bitmask.new(masks,{:cat=>true})bitmask.to_i.to_s(2)# => "1"bitmask.set(:dog,true).set(:cat,false).set(:fish,true)bitmask.to_i.to_s(2)# => "110"- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Added some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request