class Eth::Rlp::Sedes::List
A sedes type for lists of fixed length.
def deserialize(serial)
-
(DeserializationError)
- if provided serial is of wrong length. -
(DeserializationError)
- if provided serial is not a sequence.
Returns:
-
(Array)
- a deserialized list.
Parameters:
-
serial
(Array
) -- the serialized list.
def deserialize(serial) raise DeserializationError, "Can only deserialize sequences" unless Util.list?(serial) raise DeserializationError, "List has wrong length" if @strict && serial.size != self.size result = [] len = [serial.size, self.size].min len.times do |i| sedes = self[i] element = serial[i] result.push sedes.deserialize(element) end result.freeze end
def initialize(elements: [], strict: true)
-
strict
(Boolean
) -- an option to enforce the given structure. -
elements
(Array
) -- an array indicating the structure of the list.
def initialize(elements: [], strict: true) super() @strict = strict elements.each do |e| if Sedes.sedes?(e) push e elsif Util.list?(e) push List.new(elements: e) else raise TypeError, "Instances of List must only contain sedes objects or nested sequences thereof." end end end
def serialize(obj)
-
(SerializationError)
- if provided array is of wrong length. -
(SerializationError)
- if provided array is not a sequence.
Returns:
-
(Array)
- a serialized list.
Parameters:
-
obj
(Array
) -- the array to be serialized.
def serialize(obj) raise SerializationError, "Can only serialize sequences" unless Util.list?(obj) raise SerializationError, "List has wrong length" if (@strict && self.size != obj.size) || self.size < obj.size result = [] obj.zip(self).each_with_index do |(element, sedes), i| result.push sedes.serialize(element) end result end