Module: StructPacking::Packable

Includes:
Base
Defined in:
lib/struct_packing/packable.rb

Overview

Packable module provides object-packing function.

A class include this module, and call struct defining method, pack method returns defined byte-array-form of that class’s instance.

SYNOPSIS:

class PackableOStruct < OpenStruct
  include StructPacking::Packable
  self.byte_format = "int foo; char bar; byte[1] baz;"
end

obj = PackableOStruct.new
obj.foo = 1
obj.bar = 2
obj.baz = [8]
packed_bytes = obj.pack # => "\x01\x00\x00\x00\x02\b"

Instance Method Summary collapse

Methods included from Base

#pack_template

Instance Method Details

#packObject

Pack this object to byte array.

If attribute defined in byte_format, but object has no attr_getter, treat as the attribute is zero.



62
63
64
65
66
# File 'lib/struct_packing/packable.rb', line 62

def pack()
  values = struct_values
  values.flatten!
  values.pack( pack_template )
end