Module: Hexdump::ModuleMethods

Included in:
Hexdump, Kernel
Defined in:
lib/hexdump/module_methods.rb

Overview

Provides the ModuleMethods#hexdump top-level method that can be extended into modules.

module Context

  extend Hexdump::ModuleMethods

end

Instance Method Summary collapse

Instance Method Details

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

Hexdumps the given data.

Examples:

hexdump("hello\0")
# 00000000  68 65 6c 6c 6f 00                                |hello.|
# 00000006

Hexdumping to a custom output:

File.open('hexdump.txt') do |output|
  hexdump("hello\0", output: output)
end

Hexdumping to an Array:

lines = []
hexdump("hello\0", output: lines)

Hexdumping with ANSI styling:

Hexdump.hexdump(style: {index: :white, numeric: :green, chars: :cyan})

Hexdumping with ANSI highlighting:

Hexdump.hexdump("hello\0", highlights: {
                             index: {/00$/ => [:white, :bold]},
                             numeric: {
                               /^[8-f][0-9a-f]$/ => :faint,
                               /f/  => :cyan,
                               '00' => [:black, :on_red]
                             },
                             chars: {/[^\.]+/ => :green}
                           })

Configuring the hexdump with a block:

Hexdump.hexdump("hello\0") do |hexdump|
  hexdump.type = :uint16
  # ...

  hexdump.theme do |theme|
    theme.index.highlight(/00$/, [:white, :bold])
    theme.numeric.highlight(/^[8-f][0-9a-f]$/, :faint)
    # ...
  end
end

Parameters:

  • data (#each_byte)

    The data to be hexdumped.

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

  • :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:

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.



126
127
128
129
130
# File 'lib/hexdump/module_methods.rb', line 126

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

  hexdump.hexdump(data, output: output)
end