Class: Bithour::Range
- Inherits:
-
Object
- Object
- Bithour::Range
- Defined in:
- lib/bithour/range.rb
Instance Method Summary collapse
- #add(hours) ⇒ Object
- #include?(hour) ⇒ Boolean
-
#initialize(max = 24) ⇒ Range
constructor
A new instance of Range.
- #remove(hours) ⇒ Object
- #to_a ⇒ Object
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_a ⇒ Object
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 |