Class: PacketGen::StructFu::Int

Inherits:
Struct
  • Object
show all
Defined in:
lib/packetgen/structfu.rb

Overview

Ints all have a value, an endianness, and a default value. Note that the signedness of Int values are implicit as far as the subclasses are concerned; to_i and to_f will return Integer/Float versions of the input value, instead of attempting to unpack the pack value. (This can be a useful hint to other functions).

Header Definition

Fixnum  :value
Symbol  :endian
Fixnum  :width
Fixnum  :default

Direct Known Subclasses

Int16, Int32, Int64, Int8

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(value = nil, endian = nil, width = nil, default = nil) ⇒ Int

Returns a new instance of Int.



127
128
129
# File 'lib/packetgen/structfu.rb', line 127

def initialize(value=nil, endian=nil, width=nil, default=nil)
  super(value,endian,width,default=0)
end

Instance Attribute Details

#defaultObject Also known as: d

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



102
103
104
# File 'lib/packetgen/structfu.rb', line 102

def default
  @default
end

#endianObject Also known as: e

Returns the value of attribute endian

Returns:

  • (Object)

    the current value of endian



102
103
104
# File 'lib/packetgen/structfu.rb', line 102

def endian
  @endian
end

#valueObject Also known as: v

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



102
103
104
# File 'lib/packetgen/structfu.rb', line 102

def value
  @value
end

#widthObject Also known as: w

Returns the value of attribute width

Returns:

  • (Object)

    the current value of width



102
103
104
# File 'lib/packetgen/structfu.rb', line 102

def width
  @width
end

Instance Method Details

#read(i) ⇒ Object

Reads either an Integer or a packed string, and populates the value accordingly.



132
133
134
135
# File 'lib/packetgen/structfu.rb', line 132

def read(i)
  self.v = i.kind_of?(Integer) ? i.to_i : i.to_s.unpack(@packstr).first
  self
end

#to_fObject

Returns the Int as a Float.



123
124
125
# File 'lib/packetgen/structfu.rb', line 123

def to_f
  (self.v || self.d).to_f
end

#to_iObject

Returns the Int as an Integer.



118
119
120
# File 'lib/packetgen/structfu.rb', line 118

def to_i
  (self.v || self.d).to_i
end

#to_sObject

This is a parent class definition and should not be used directly.

Raises:

  • (StandardError)


113
114
115
# File 'lib/packetgen/structfu.rb', line 113

def to_s
  raise StandardError, "StructFu::Int#to_s accessed, must be redefined."
end