Class: StructFu::Int
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
Integer :value
Symbol :endian
Integer :width
Integer :default
Instance Attribute Summary collapse
-
#default ⇒ Object
(also: #d)
Returns the value of attribute default.
-
#endian ⇒ Object
(also: #e)
Returns the value of attribute endian.
-
#value ⇒ Object
(also: #v)
Returns the value of attribute value.
-
#width ⇒ Object
(also: #w)
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(value = nil, endian = nil, width = nil, default = nil) ⇒ Int
constructor
A new instance of Int.
-
#read(i) ⇒ Object
Reads either an Integer or a packed string, and populates the value accordingly.
-
#to_f ⇒ Object
Returns the Int as a Float.
-
#to_i ⇒ Object
Returns the Int as an Integer.
-
#to_s ⇒ Object
This is a parent class definition and should not be used directly.
Methods inherited from Struct
Constructor Details
#initialize(value = nil, endian = nil, width = nil, default = nil) ⇒ Int
Returns a new instance of Int.
84 85 86 |
# File 'lib/packetfu/structfu.rb', line 84 def initialize(value=nil, endian=nil, width=nil, default=nil) super(value,endian,width,default=0) end |
Instance Attribute Details
#default ⇒ Object Also known as: d
Returns the value of attribute default
59 60 61 |
# File 'lib/packetfu/structfu.rb', line 59 def default @default end |
#endian ⇒ Object Also known as: e
Returns the value of attribute endian
59 60 61 |
# File 'lib/packetfu/structfu.rb', line 59 def endian @endian end |
#value ⇒ Object Also known as: v
Returns the value of attribute value
59 60 61 |
# File 'lib/packetfu/structfu.rb', line 59 def value @value end |
#width ⇒ Object Also known as: w
Returns the value of attribute width
59 60 61 |
# File 'lib/packetfu/structfu.rb', line 59 def width @width end |
Instance Method Details
#read(i) ⇒ Object
Reads either an Integer or a packed string, and populates the value accordingly.
89 90 91 92 |
# File 'lib/packetfu/structfu.rb', line 89 def read(i) self.v = i.kind_of?(Integer) ? i.to_i : i.to_s.unpack(@packstr).first self end |
#to_f ⇒ Object
Returns the Int as a Float.
80 81 82 |
# File 'lib/packetfu/structfu.rb', line 80 def to_f (self.v || self.d).to_f end |
#to_i ⇒ Object
Returns the Int as an Integer.
75 76 77 |
# File 'lib/packetfu/structfu.rb', line 75 def to_i (self.v || self.d).to_i end |
#to_s ⇒ Object
This is a parent class definition and should not be used directly.
70 71 72 |
# File 'lib/packetfu/structfu.rb', line 70 def to_s raise StandardError, "StructFu::Int#to_s accessed, must be redefined." end |