Class: Bytepack::Hash

Inherits:
Complex show all
Defined in:
lib/bytepack/complex/hash.rb

Class Method Summary collapse

Methods inherited from Struct

classifyDataType, config, packingDataType, single_type_array?, testpacking

Class Method Details

.pack(hash = {}) ⇒ Object



5
6
7
# File 'lib/bytepack/complex/hash.rb', line 5

def pack(hash = {})
  AnyType.pack(hash.keys) + AnyType.pack(hash.values)
end

.unpack(bytes, offset = 0) ⇒ Object



9
10
11
12
13
# File 'lib/bytepack/complex/hash.rb', line 9

def unpack(bytes, offset = 0)
  keys, offset = *AnyType.unpack(bytes, offset)
  values, offset = *AnyType.unpack(bytes, offset)
  [::Hash[keys.map.with_index {|key, index| [key, values[index]]}], offset]
end