Class: BinaryCodec::BytesList

Inherits:
Object
  • Object
show all
Defined in:
lib/binary-codec/serdes/bytes_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBytesList

Returns a new instance of BytesList.



8
9
10
# File 'lib/binary-codec/serdes/bytes_list.rb', line 8

def initialize
  @bytes_array = []
end

Instance Attribute Details

#bytes_arrayObject (readonly)

Returns the value of attribute bytes_array.



6
7
8
# File 'lib/binary-codec/serdes/bytes_list.rb', line 6

def bytes_array
  @bytes_array
end

Instance Method Details

#get_lengthInteger

Returns the total length of all bytes in the list.

Returns:

  • (Integer)

    The total length.



14
15
16
# File 'lib/binary-codec/serdes/bytes_list.rb', line 14

def get_length
  @bytes_array.inject(0) { |sum, arr| sum + arr.length }
end

#put(bytes_arg) ⇒ BytesList

Adds bytes to the list.

Parameters:

  • bytes_arg (Array<Integer>)

    The bytes to add.

Returns:



21
22
23
24
25
# File 'lib/binary-codec/serdes/bytes_list.rb', line 21

def put(bytes_arg)
  bytes = bytes_arg.dup
  @bytes_array << bytes
  self # Allow chaining
end

#to_byte_sink(list) ⇒ Object

Puts the bytes into another byte sink.

Parameters:

  • list (Object)

    The sink to put bytes into.



29
30
31
# File 'lib/binary-codec/serdes/bytes_list.rb', line 29

def to_byte_sink(list)
  list.put(to_bytes)
end

#to_bytesArray<Integer>

Returns all bytes as a single flat array.

Returns:

  • (Array<Integer>)

    The flattened byte array.



35
36
37
# File 'lib/binary-codec/serdes/bytes_list.rb', line 35

def to_bytes
  @bytes_array.flatten # TODO: Uses concat in xrpl.js, maybe implement that instead
end

#to_hexString

Returns the hex representation of all bytes in the list.

Returns:

  • (String)

    The hex string.



41
42
43
# File 'lib/binary-codec/serdes/bytes_list.rb', line 41

def to_hex
  bytes_to_hex(to_bytes)
end