Permalink
Browse files

Integer bits can be addressed using array syntax:

    n = 21
    n[0] # => 1
    n[1] # => 0
    n[2] # => 1
    # etc..

To generate an array of bits from an integer:

    number_of_bits = Math.sqrt(n).ceil
    # => 5

    number_of_bits.times.map { |i| n[i] }
    # => [1, 0, 1, 0, 1]
  • Loading branch information...
1 parent ed2e043 commit 435129033cb8662682ba204e917c4f838e0896a5 @jeriko jeriko committed Nov 26, 2011
Showing with 2 additions and 1 deletion.
  1. +2 −1 lib/methods/bitmask.rb
View
@@ -9,8 +9,9 @@ def initialize(base, column_name, options)
base.send :define_method, :_roles do
states = base.const_get(column_name.upcase.to_sym)
+ masked_integer = self[column_name.to_sym] || 0
- states.reject { |r| ((self[column_name.to_sym] || 0) & 2**states.index(r)).zero? }
+ states.reject.with_index { |r,i| masked_integer[i].zero? }
end
base.send :define_method, :has_role? do |role|

0 comments on commit 4351290

Please sign in to comment.