Class: Bithour::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/bithour/range.rb

Instance Method Summary collapse

Constructor Details

#initialize(max = 24) ⇒ Range



3
4
5
6
# File 'lib/bithour/range.rb', line 3

def initialize(max=24)
  @hour_bits = 0
  @max = max
end

Instance Method Details

#add(hours) ⇒ Object



8
9
10
# File 'lib/bithour/range.rb', line 8

def add(hours)
  update(hours, "1")
end

#include?(hour) ⇒ Boolean



16
17
18
# File 'lib/bithour/range.rb', line 16

def include?(hour)
  @hour_bits[hour] == 1
end

#remove(hours) ⇒ Object



12
13
14
# File 'lib/bithour/range.rb', line 12

def remove(hours)
  update(hours, "0")
end

#to_aObject



20
21
22
23
24
25
# 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