Method: Binary::Protocol::ClassMethods#stringz

Defined in:
lib/binary/protocol.rb

#stringz(name, options = {}) ⇒ Object

Declare a null terminated string field.

Examples:

class Message
  include Binary::Protocol
  stringz :collection
end

Parameters:

  • name (String)

    the name of this field



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/binary/protocol.rb', line 174

def stringz(name, options = {})
  if options.key?(:always)
    __define_always__(name, options[:always])
  else
    if options.key?(:default)
      __define_default__(name, options[:default])
    else
      attr_accessor name
    end

    class_eval "      def deserialize_\#{name}(buffer)\n        raise NotImplementedError\n      end\n    RUBY\n  end\n\n  class_eval <<-RUBY, __FILE__, __LINE__ + 1\n    def serialize_\#{name}(buffer)\n      buffer << \#{name}\n      buffer << 0\n    end\n  RUBY\n\n  serialization << :\"serialize_\#{name}\"\n  fields << name\nend\n", __FILE__, __LINE__ + 1