Class: EacRubyUtils::BitArray
Class Method Summary collapse
Instance Method Summary collapse
- #<<(value) ⇒ Object
-
#initialize(values = []) ⇒ BitArray
constructor
A new instance of BitArray.
- #push(value) ⇒ EacRubyUtils::Bit
- #push_array(other_bit_array) ⇒ Object
- #reverse ⇒ EacRubyUtils::BitArray
- #to_byte_array(big_endian = false) ⇒ EacRubyUtils::ByteArray
- #to_int_array ⇒ Array<Integer>
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
36 37 38 |
# File 'lib/eac_ruby_utils/bit_array.rb', line 36 def push(value) values_array.push(::EacRubyUtils::Bit.assert(value)) end |
#push_array(other_bit_array) ⇒ Object
30 31 32 |
# File 'lib/eac_ruby_utils/bit_array.rb', line 30 def push_array(other_bit_array) @values_array += other_bit_array.values_array end |
#reverse ⇒ EacRubyUtils::BitArray
41 42 43 |
# File 'lib/eac_ruby_utils/bit_array.rb', line 41 def reverse self.class.new(values_array.reverse) end |
#to_byte_array(big_endian = false) ⇒ EacRubyUtils::ByteArray
47 48 49 50 51 52 53 54 55 |
# File 'lib/eac_ruby_utils/bit_array.rb', line 47 def to_byte_array(big_endian = false) 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_array ⇒ Array<Integer>
58 59 60 |
# File 'lib/eac_ruby_utils/bit_array.rb', line 58 def to_int_array values_array.map(&:to_i) end |