Class: PacketGen::Types::Int Abstract
- Inherits:
-
Object
- Object
- PacketGen::Types::Int
- Defined in:
- lib/packetgen/types/int.rb
Overview
This class is abstract.
Base integer class to handle binary integers
Instance Attribute Summary collapse
-
#default ⇒ Integer
Integer default value.
-
#endian ⇒ :little, :big
Integer endianness.
-
#value ⇒ Integer
Integer value.
-
#width ⇒ Integer
Integer size, in bytes.
Instance Method Summary collapse
-
#initialize(value = nil, endian = nil, width = nil, default = 0) ⇒ Int
constructor
A new instance of Int.
-
#read(value) ⇒ self
abstract
Read an Int from a binary string or an integer.
-
#sz ⇒ Integer
Give size in bytes of self.
-
#to_f ⇒ Float
Convert Int to Float.
-
#to_i ⇒ Integer
(also: #to_human)
Convert Int to Integer.
- #to_s ⇒ ::String abstract
Constructor Details
#initialize(value = nil, endian = nil, width = nil, default = 0) ⇒ Int
Returns a new instance of Int.
32 33 34 35 36 37 |
# File 'lib/packetgen/types/int.rb', line 32 def initialize(value=nil, endian=nil, width=nil, default=0) @value = value @endian = endian @width = width @default = default end |
Instance Attribute Details
#default ⇒ Integer
Integer default value
26 27 28 |
# File 'lib/packetgen/types/int.rb', line 26 def default @default end |
#endian ⇒ :little, :big
Integer endianness
20 21 22 |
# File 'lib/packetgen/types/int.rb', line 20 def endian @endian end |
#value ⇒ Integer
Integer value
17 18 19 |
# File 'lib/packetgen/types/int.rb', line 17 def value @value end |
#width ⇒ Integer
Integer size, in bytes
23 24 25 |
# File 'lib/packetgen/types/int.rb', line 23 def width @width end |
Instance Method Details
#read(value) ⇒ self
This method is abstract.
Read an Int from a binary string or an integer
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/packetgen/types/int.rb', line 44 def read(value) @value = if value.is_a?(Integer) value.to_i elsif defined? @packstr value.to_s.unpack(@packstr[@endian]).first else raise ParseError, 'Int#read is abstract and cannot read' end self end |
#sz ⇒ Integer
Give size in bytes of self
82 83 84 |
# File 'lib/packetgen/types/int.rb', line 82 def sz width end |
#to_f ⇒ Float
Convert Int to Float
76 77 78 |
# File 'lib/packetgen/types/int.rb', line 76 def to_f to_i.to_f end |
#to_i ⇒ Integer Also known as: to_human
Convert Int to Integer
68 69 70 |
# File 'lib/packetgen/types/int.rb', line 68 def to_i @value || @default end |
#to_s ⇒ ::String
This method is abstract.
58 59 60 61 62 63 64 |
# File 'lib/packetgen/types/int.rb', line 58 def to_s unless defined? @packstr raise ParseError, 'PacketGen::Types::Int#to_s is an abstract method' end [to_i].pack(@packstr[@endian]) end |