Module: IOStruct::NestedInstanceMethods

Defined in:
lib/iostruct.rb

Overview

initialize nested structures / arrays

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/iostruct.rb', line 114

def initialize *args
  super
  self.class::FIELDS.each do |k, v|
    next unless v.fmt

    if (value = self[k])
      self[k] = v.fmt.is_a?(String) ? value.unpack(v.fmt) : v.fmt.read(value)
    end
  end
end

#packObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/iostruct.rb', line 125

def pack
  values = self.class::FIELDS.map do |k, v|
    value = self[k]
    next value unless v&.fmt && value

    # Reverse the unpacking done in initialize
    v.fmt.is_a?(String) ? value.pack(v.fmt) : value.pack
  end
  values.pack self.class.const_get('FORMAT')
end