Skip to content

Commit

Permalink
Integer bits can be addressed using array syntax:
Browse files Browse the repository at this point in the history
    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
Daniel Davey committed Nov 26, 2011
1 parent ed2e043 commit 4351290
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/methods/bitmask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 4351290

Please sign in to comment.