Module: Hexdump::Mixin

Included in:
IO, String, StringIO
Defined in:
lib/hexdump/mixin.rb

Overview

Provides a Mixin#hexdump method that can be included into objects.

class DataObject

  include Hexdump::Mixin

  def each_byte
    # ...
  end

end

data.hexdump

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#hexdump(output: $stdout, **kwargs) {|hexdump| ... } ⇒ Object

Prints a hexdumps of the object.

Examples:

obj.hexdump
# ...

Hexdumping to a custom output:

File.open('hexdump.txt') do |output|
  obj.hexdump(output: output)
end

Hexdumping to an Array:

lines = []
obj.hexdump(output: lines)

Parameters:

  • output (#print) (defaults to: $stdout)

    ($stdout) The output to print the hexdump to.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for Hexdump#initialize.

Options Hash (**kwargs):

  • :type (:int8, :uint8, :char, :uchar, :byte, :int16, :int16_le, :int16_be, :int16_ne, :uint16, :uint16_le, :uint16_be, :uint16_ne, :short, :short_le, :short_be, :short_ne, :ushort, :ushort_le, :ushort_be, :ushort_ne, :int32, :int32_le, :int32_be, :int32_ne, :uint32, :uint32_le, :uint32_be, :uint32_ne, :int, :long, :long_le, :long_be, :long_ne, :uint, :ulong, :ulong_le, :ulong_be, :ulong_ne, :int64, :int64_le, :int64_be, :int64_ne, :uint64, :uint64_le, :uint64_be, :uint64_ne, :long_long, :long_long_le, :long_long_be, :long_long_ne, :ulong_long, :ulong_long_le, :ulong_long_be, :ulong_long_ne, :float, :float_le, :float_be, :float_ne, :double, :double_le, :double_be, :double_ne) — default: :byte

    The type to decode the data as.

  • :offset (Integer, nil)

    Controls whether to skip N number of bytes before starting to read data.

  • :length (Integer, nil)

    Controls control many bytes to read.

  • :zero_pad (Boolean) — default: false

    Enables or disables zero padding of data, so that the remaining bytes can be decoded as a uint, int, or float.

  • :columns (Integer) — default: 16

    The number of bytes to dump for each line.

  • :group_columns (Integer, nil)

    Separate groups of columns with an additional space.

  • :group_chars (Integer, :type, nil)

    Group chars into columns. If :type, then the chars will be grouped by the type's size.

  • :repeating (Boolean)

    Controls whether to omit repeating duplicate rows data with a *.

  • :base (16, 10, 8, 2) — default: 16

    The base to print bytes in.

  • :index_base (16, 10, 8, 2) — default: 16

    Control the base that the index is displayed in.

  • :chars_column (Boolean) — default: true

    Controls whether to display the characters column.

  • :encoding (:ascii, :utf8, Encoding, nil)

    The encoding to display the characters in.

  • :index_offset (Integer, nil)

    The offset to start the index at.

  • :style (Boolean, Hash{:index,:numeric,:chars => Symbol,Array<Symbol>})

    Enables theming of index, numeric, or chars columns.

  • :highlights (Boolean, Hash{:index,:numeric,:chars => Hash{String,Regexp => Symbol,Array<Symbol>}})

    Enables selective highlighting of index, numeric, or chars columns.

Yields:

  • (hexdump)

    If a block is given, it will be passed the newly initialized hexdump instance.

Yield Parameters:

Raises:

  • (ArgumentError)

    The given data does not define the #each_byte method, the :output value does not support the #<< method or the :base value was unknown.

Since:

  • 1.0.0



105
106
107
108
109
# File 'lib/hexdump/mixin.rb', line 105

def hexdump(output: $stdout, **kwargs,&block)
  hexdump = ::Hexdump::Hexdump.new(**kwargs,&block)

  hexdump.hexdump(self, output: output)
end

#to_hexdump(**kwargs) {|hexdump| ... } ⇒ String

Note:

Caution: this method appends each line of the hexdump to a String, and that String can grow quite large and consume a lot of memory.

Outputs the hexdump to a String.

Examples:

obj.hexdump
# => "..."

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for Hexdump#initialize.

Options Hash (**kwargs):

  • :type (:int8, :uint8, :char, :uchar, :byte, :int16, :int16_le, :int16_be, :int16_ne, :uint16, :uint16_le, :uint16_be, :uint16_ne, :short, :short_le, :short_be, :short_ne, :ushort, :ushort_le, :ushort_be, :ushort_ne, :int32, :int32_le, :int32_be, :int32_ne, :uint32, :uint32_le, :uint32_be, :uint32_ne, :int, :long, :long_le, :long_be, :long_ne, :uint, :ulong, :ulong_le, :ulong_be, :ulong_ne, :int64, :int64_le, :int64_be, :int64_ne, :uint64, :uint64_le, :uint64_be, :uint64_ne, :long_long, :long_long_le, :long_long_be, :long_long_ne, :ulong_long, :ulong_long_le, :ulong_long_be, :ulong_long_ne, :float, :float_le, :float_be, :float_ne, :double, :double_le, :double_be, :double_ne) — default: :byte

    The type to decode the data as.

  • :offset (Integer, nil)

    Controls whether to skip N number of bytes before starting to read data.

  • :length (Integer, nil)

    Controls control many bytes to read.

  • :zero_pad (Boolean) — default: false

    Enables or disables zero padding of data, so that the remaining bytes can be decoded as a uint, int, or float.

  • :columns (Integer) — default: 16

    The number of bytes to dump for each line.

  • :group_columns (Integer, nil)

    Separate groups of columns with an additional space.

  • :group_chars (Integer, :type, nil)

    Group chars into columns. If :type, then the chars will be grouped by the type's size.

  • :repeating (Boolean)

    Controls whether to omit repeating duplicate rows data with a *.

  • :base (16, 10, 8, 2) — default: 16

    The base to print bytes in.

  • :index_base (16, 10, 8, 2) — default: 16

    Control the base that the index is displayed in.

  • :index_offset (Integer, nil)

    The offset to start the index at.

  • :chars_column (Boolean) — default: true

    Controls whether to display the characters column.

  • :encoding (:ascii, :utf8, Encoding, nil)

    The encoding to display the characters in.

  • :style (Boolean, Hash{:index,:numeric,:chars => Symbol,Array<Symbol>})

    Enables theming of index, numeric, or chars columns.

  • :highlights (Boolean, Hash{:index,:numeric,:chars => Hash{String,Regexp => Symbol,Array<Symbol>}})

    Enables selective highlighting of index, numeric, or chars columns.

Yields:

  • (hexdump)

    If a block is given, it will be passed the newly initialized hexdump instance.

Yield Parameters:

Returns:

  • (String)

    The output of the hexdump.

Raises:

  • (ArgumentError)

    The given data does not define the #each_byte method, the :base value was unknown.

Since:

  • 1.0.0



186
187
188
189
190
# File 'lib/hexdump/mixin.rb', line 186

def to_hexdump(**kwargs,&block)
  hexdump = ::Hexdump::Hexdump.new(**kwargs,&block)

  hexdump.dump(self)
end