class Bithour::Range
Public Class Methods
new(max=24)
click to toggle source
# File lib/bithour/range.rb, line 3 def initialize(max=24) @hour_bits = 0 @max = max end
Public Instance Methods
add(hours)
click to toggle source
# File lib/bithour/range.rb, line 8 def add(hours) update(hours, "1") end
include?(hour)
click to toggle source
# File lib/bithour/range.rb, line 16 def include?(hour) @hour_bits[hour] == 1 end
remove(hours)
click to toggle source
# File lib/bithour/range.rb, line 12 def remove(hours) update(hours, "0") end
to_a()
click to toggle source
# File lib/bithour/range.rb, line 20 def to_a bits = ("%0#{@max}d" % @hour_bits.to_s(2)).reverse.split(//) hours = bits.collect.with_index {|hour, i| [i, hour]} hours.select! {|hour| hour[1] == "1"} hours.collect {|hour, bit| hour} end
Private Instance Methods
update(_hours, bit)
click to toggle source
# File lib/bithour/range.rb, line 28 def update(_hours, bit) if _hours.respond_to?(:each) hours = _hours else hours = [_hours] end hour_str = "%0#{@max}d" % @hour_bits.to_s(2) hour_str.reverse! hours.each do |i| hour_str[i % @max] = bit end @hour_bits = hour_str.reverse.to_i(2) end