Class: BinStruct::Int Abstract

Inherits:
Object
  • Object
show all
Includes:
Structable
Defined in:
lib/bin_struct/int.rb

Overview

This class is abstract.

Base integer class to handle binary integers

Author:

  • Sylvain Daubert (2016-2024)

  • LemonTree55

Direct Known Subclasses

Enum, Int16, Int24, Int32, Int64, Int8, SInt8

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Structable

#type_name

Constructor Details

#initialize(options = {}) ⇒ Int

Returns a new instance of Int.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :value (Integer, nil)

    Value to set Int to

  • :endian (:little, :big, :native, nil)

    Int’s endianess

  • :width (Integer, nil)

    Int’s width in bytes

  • :default (Integer)

    Default value to use when #value is not set (Default to 0).

Author:

  • LemonTree55



36
37
38
39
40
41
# File 'lib/bin_struct/int.rb', line 36

def initialize(options = {})
  @value = options[:value]
  @endian = options[:endian]
  @width = options[:width] || 0
  @default = options[:default] || 0
end

Instance Attribute Details

#defaultInteger

Integer default value

Returns:

  • (Integer)


28
29
30
# File 'lib/bin_struct/int.rb', line 28

def default
  @default
end

#endian:little, ...

Integer endianness

Returns:

  • (:little, :big, :native, nil)


22
23
24
# File 'lib/bin_struct/int.rb', line 22

def endian
  @endian
end

#valueInteger?

Integer value

Returns:

  • (Integer, nil)


19
20
21
# File 'lib/bin_struct/int.rb', line 19

def value
  @value
end

#widthInteger

Integer size, in bytes

Returns:

  • (Integer)


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

Returns:

  • (::String)


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.

Parameters:

  • value (Integer)

Returns:

  • (self)


75
76
77
78
# File 'lib/bin_struct/int.rb', line 75

def from_human(value)
  @value = value
  self
end

#nbitsInteger

Return the number of bits used to encode this Int

Returns:

  • (Integer)


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

Parameters:

Returns:

  • (self)

Raises:

  • (Error)

    when reading #to_s objects with abstract Int class.

Author:

  • LemonTree55



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

#szInteger

Give size in bytes of self

Returns:

  • (Integer)


88
89
90
# File 'lib/bin_struct/int.rb', line 88

def sz
  width
end

#to_fFloat

Convert Int to Float

Returns:

  • (Float)


82
83
84
# File 'lib/bin_struct/int.rb', line 82

def to_f
  to_i.to_f
end

#to_iInteger Also known as: to_human

Convert Int to Integer

Returns:

  • (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.

Returns:

  • (::String)

Raises:

  • (Error)

    This is an abstrat method and must be redefined



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