class Flagpole
Constants
- VERSION
Public Class Methods
Public: Initialize the set of flags.
value - An Integer with the initial value of the set. flags - The name of the flags you’re using.
# File lib/flagpole.rb, line 6 def initialize(value = 0, flags) @flags = flags.zip(bitmap(value, flags.size)).to_h end
Public Instance Methods
Public: Get a specific flag by name.
key - One of the flag names passed to the initializer.
Returns Boolean.
# File lib/flagpole.rb, line 15 def [](key) @flags.fetch(key) end
Public: Set a specific flag by name.
flag - One of the flag names passed to the initializer. value - A Boolean.
Returns nothing. Raises ArgumentError if passed a flag that isn’t in the set.
# File lib/flagpole.rb, line 26 def []=(flag, val) fail ArgumentError, "#{flag} isn't a valid flag" unless @flags.key?(flag) @flags[flag] = val end
Public: Returns a Hash with all the flags and their current values.
# File lib/flagpole.rb, line 39 def to_h @flags end
Public: Returns the current value of the flag set as an Integer.
# File lib/flagpole.rb, line 32 def to_i @flags.each_value.with_index.inject(0) do |flag, (bit, power)| flag | (bit ? 1 : 0) * 2**power end end
Public: Find the integer value of a single flag, regardless of whether it is set or not. This value can then be added/substracted from the integer value of this set for low-level manipulation of the flag set.
flag - The name of a flag.
Returns an Integer. Raises ArgumentError if passed a flag that isn’t in the set.
# File lib/flagpole.rb, line 51 def value_of(flag) fail ArgumentError, "#{flag} isn't a valid flag" unless @flags.key?(flag) 2 ** @flags.keys.index(flag) end
Private Instance Methods
Internal: Generate the list of bits that make a given number. Each index of the array corresponds to the bit for 2**index.
Returns an Array of Booleans.
# File lib/flagpole.rb, line 60 def bitmap(num, size) Array.new(size).map.with_index { |_, i| num[i] == 1 } end