Class: FrequencyEnumerator::Decomposer

Inherits:
Object
  • Object
show all
Defined in:
lib/frequency_enumerator/decomposer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Decomposer

Returns a new instance of Decomposer.



9
10
11
12
# File 'lib/frequency_enumerator/decomposer.rb', line 9

def initialize(params = {})
  @bit_count  = params[:bit_count]  || 8
  @endianness = params[:endianness] || :little
end

Instance Attribute Details

#bit_countObject (readonly)

Returns the value of attribute bit_count.



6
7
8
# File 'lib/frequency_enumerator/decomposer.rb', line 6

def bit_count
  @bit_count
end

#endiannessObject (readonly)

Returns the value of attribute endianness.



7
8
9
# File 'lib/frequency_enumerator/decomposer.rb', line 7

def endianness
  @endianness
end

Class Method Details

.decompose(integer) ⇒ Object



14
15
16
# File 'lib/frequency_enumerator/decomposer.rb', line 14

def self.decompose(integer)
  new.decompose(integer)
end

Instance Method Details

#big_endian?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/frequency_enumerator/decomposer.rb', line 31

def big_endian?
  endianess == :big
end

#decompose(integer) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/frequency_enumerator/decomposer.rb', line 18

def decompose(integer)
  raise_if_negative(integer)
  raise_if_not_enough_bits(integer)

  bit_array = bit_count.times.map { |b| integer[b] }

  little_endian? ? bit_array : bit_array.reverse
end

#little_endian?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/frequency_enumerator/decomposer.rb', line 27

def little_endian?
  endianness == :little
end