Class: BinStruct::Int Abstract
- Inherits:
-
Object
- Object
- BinStruct::Int
- Includes:
- Structable
- Defined in:
- lib/bin_struct/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, ...
Integer endianness.
-
#value ⇒ Integer?
Integer value.
-
#width ⇒ Integer
Integer size, in bytes.
Instance Method Summary collapse
-
#format_inspect ⇒ ::String
Format Int type when inspecting Struct.
-
#from_human(value) ⇒ self
Initialize value from an Integer.
-
#initialize(options = {}) ⇒ Int
constructor
A new instance of Int.
-
#nbits ⇒ Integer
Return the number of bits used to encode this Int.
-
#read(str) ⇒ 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
Methods included from Structable
Constructor Details
#initialize(options = {}) ⇒ Int
Returns a new instance of Int.
36 37 38 39 40 41 |
# File 'lib/bin_struct/int.rb', line 36 def initialize( = {}) @value = [:value] @endian = [:endian] @width = [:width] || 0 @default = [:default] || 0 end |
Instance Attribute Details
#default ⇒ Integer
Integer default value
28 29 30 |
# File 'lib/bin_struct/int.rb', line 28 def default @default end |
#endian ⇒ :little, ...
Integer endianness
22 23 24 |
# File 'lib/bin_struct/int.rb', line 22 def endian @endian end |
#value ⇒ Integer?
Integer value
19 20 21 |
# File 'lib/bin_struct/int.rb', line 19 def value @value end |
#width ⇒ Integer
Integer size, in bytes
25 26 27 |
# File 'lib/bin_struct/int.rb', line 25 def width @width end |
Instance Method Details
#format_inspect ⇒ ::String
Format Int type when inspecting Struct
94 95 96 |
# File 'lib/bin_struct/int.rb', line 94 def format_inspect format_str % [to_i.to_s, to_i] end |
#from_human(value) ⇒ self
Initialize value from an Integer.
75 76 77 78 |
# File 'lib/bin_struct/int.rb', line 75 def from_human(value) @value = value self end |
#nbits ⇒ Integer
Return the number of bits used to encode this Int
100 101 102 |
# File 'lib/bin_struct/int.rb', line 100 def nbits width * 8 end |
#read(str) ⇒ self
This method is abstract.
Read an Int from a binary string or an integer
49 50 51 52 53 54 |
# File 'lib/bin_struct/int.rb', line 49 def read(str) raise Error, 'BinStruct::Int#read is abstract' unless defined? @packstr @value = str.to_s.unpack1(@packstr[@endian]) self end |
#sz ⇒ Integer
Give size in bytes of self
88 89 90 |
# File 'lib/bin_struct/int.rb', line 88 def sz width end |
#to_f ⇒ Float
Convert Int to Float
82 83 84 |
# File 'lib/bin_struct/int.rb', line 82 def to_f to_i.to_f end |
#to_i ⇒ Integer Also known as: to_human
Convert Int to Integer
67 68 69 |
# File 'lib/bin_struct/int.rb', line 67 def to_i @value || @default end |
#to_s ⇒ ::String
This method is abstract.
59 60 61 62 63 |
# File 'lib/bin_struct/int.rb', line 59 def to_s raise Error, 'BinStruct::Int#to_s is abstract' unless defined? @packstr [to_i].pack(@packstr[@endian]) end |