Class: EacRubyUtils::BitArray

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_ruby_utils/bit_array.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = []) ⇒ BitArray

Returns a new instance of BitArray.



22
23
24
# File 'lib/eac_ruby_utils/bit_array.rb', line 22

def initialize(values = [])
  values.each { |value| push(value) }
end

Class Method Details

.assert(obj) ⇒ Object



11
12
13
14
15
16
# File 'lib/eac_ruby_utils/bit_array.rb', line 11

def assert(obj)
  return obj if obj.is_a?(self)
  return new(obj) if obj.is_a?(::Enumerable)

  raise "Could not convert #{obj} to #{self}"
end

Instance Method Details

#<<(value) ⇒ Object



26
27
28
# File 'lib/eac_ruby_utils/bit_array.rb', line 26

def <<(value)
  push(value)
end

#push(value) ⇒ EacRubyUtils::Bit

Parameters:

Returns:



39
40
41
# File 'lib/eac_ruby_utils/bit_array.rb', line 39

def push(value)
  values_array.push(::EacRubyUtils::Bit.assert(value))
end

#push_array(other_bit_array) ⇒ EacRubyUtils::BitArray

Returns self.

Returns:



31
32
33
34
35
# File 'lib/eac_ruby_utils/bit_array.rb', line 31

def push_array(other_bit_array)
  values_array.push(*other_bit_array.to_a)

  self
end

#reverseEacRubyUtils::BitArray



44
45
46
# File 'lib/eac_ruby_utils/bit_array.rb', line 44

def reverse
  self.class.new(values_array.reverse)
end

#to_byte_array(big_endian = false) ⇒ EacRubyUtils::ByteArray

Parameters:

  • big_endian (Boolean) (defaults to: false)

Returns:



50
51
52
53
54
55
56
57
58
# File 'lib/eac_ruby_utils/bit_array.rb', line 50

def to_byte_array(big_endian = false) # rubocop:disable Style/OptionalBooleanParameter
  unless count.modulo(::EacRubyUtils::Byte::BIT_COUNT).zero?
    raise 'Bits returned is not multile of 8'
  end

  byte_bits_enumerator.each_with_object(::EacRubyUtils::ByteArray.new) do |e, a|
    a << ::EacRubyUtils::Byte.from_bit_array(e, big_endian)
  end
end

#to_int_arrayArray<Integer>

Returns:

  • (Array<Integer>)


61
62
63
# File 'lib/eac_ruby_utils/bit_array.rb', line 61

def to_int_array
  values_array.map(&:to_i)
end