Class: Hexdump::Numeric::Binary Private

Inherits:
FormatString show all
Defined in:
lib/hexdump/numeric/binary.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

Constant Summary collapse

SIZE_TO_WIDTH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

{
  1 => 8,  # 0xff.to_s(2).length
  2 => 16, # 0xffff.to_s(2).length
  4 => 32, # 0xffffffff.to_s(2).length
  8 => 64  # 0xffffffffffffffff.to_s(2).length
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FormatString

#%, #to_s

Constructor Details

#initialize(type) ⇒ Binary

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the binary format.

Parameters:

Raises:

Since:

  • 1.0.0



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hexdump/numeric/binary.rb', line 34

def initialize(type)
  case type
  when Type::Int, Type::UInt
    @width = SIZE_TO_WIDTH.fetch(type.size) do
      raise(NotImplementedError,"type #{type} with unsupported size #{type.size}")
    end

    if type.signed?
      super("% .#{@width}b"); @width += 1
    else
      super("%.#{@width}b")
    end
  when Type::Float
    raise(IncompatibleTypeError,"cannot format floating-point numbers in binary")
  else
    raise(TypeError,"unsupported type: #{type.inspect}")
  end
end

Instance Attribute Details

#widthInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Integer)

Since:

  • 1.0.0



21
22
23
# File 'lib/hexdump/numeric/binary.rb', line 21

def width
  @width
end