Sha256: c2b00432c8cc7e38cc1ee3438f82ac5844e1dbfcd8d98f48c041f64b324679f6
Contents?: true
Size: 1.12 KB
Versions: 1
Compression:
Stored size: 1.12 KB
Contents
module StructPacking # 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" module Packable private include Base def self.included(base) base.send("include", Base) end public # Pack this object to byte array. # # If attribute defined in byte_format, # but object has no attr_getter, treat as the attribute is zero. def pack() values = field_names.collect do |n| begin instance_eval { send(n) } rescue NoMethodError 0 end end values.flatten! values.pack( pack_template ) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
struct_packing-0.0.2 | lib/struct_packing/packable.rb |