Class: PacketGen::Types::String

Inherits:
String
  • Object
show all
Defined in:
lib/packetgen/types/string.rb

Overview

This class is just like regular String. It only adds #read and #sz methods to be compatible with others PacketGen::Types.

Author:

  • Sylvain Daubert

Instance Method Summary collapse

Constructor Details

#initialize(str = '', options = {}) ⇒ String

Returns a new instance of String.

Options Hash (options):

  • :length_from (Types::Int, Proc)

    object or proc from which takes length when reading



19
20
21
22
# File 'lib/packetgen/types/string.rb', line 19

def initialize(str='', options={})
  super(str)
  @length_from = options[:length_from]
end

Instance Method Details

#read(str) ⇒ String



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/packetgen/types/string.rb', line 26

def read(str)
  s = str.to_s
  s = case @length_from
      when Int
        s[0, @length_from.to_i]
      else
        s
      end
  self.replace s
  self
end