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
- #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
32 33 34 |
# File 'lib/eac_ruby_utils/bit_array.rb', line 32 def push(value) values_array.push(::EacRubyUtils::Bit.assert(value)) end |
#reverse ⇒ EacRubyUtils::BitArray
37 38 39 |
# File 'lib/eac_ruby_utils/bit_array.rb', line 37 def reverse self.class.new(values_array.reverse) end |
#to_byte_array(big_endian = false) ⇒ EacRubyUtils::ByteArray
43 44 45 46 47 48 49 50 51 |
# File 'lib/eac_ruby_utils/bit_array.rb', line 43 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>
54 55 56 |
# File 'lib/eac_ruby_utils/bit_array.rb', line 54 def to_int_array values_array.map(&:to_i) end |