Skip to content

class IO::Hexdump
inherits IO

IO object that prints an hexadecimal dump of all transferred data.

Especially useful for debugging binary protocols on an IO, to understand better when and how data is sent or received.

By default IO::Hexdump won't print anything; you must specify which of read, write or both you want to print.

Example:

require "io/hexdump"
socket = IO::Memory.new("abc")
io = IO::Hexdump.new(socket, output: STDERR, read: true)

When data is read from io it will print something akin to the following on STDERR:

00000000  50 52 49 20 2a 20 48 54  54 50 2f 32 2e 30 0d 0a  PRI * HTTP/2.0..
00000010  0d 0a 53 4d 0d 0a 0d 0a                           ..SM....
00000000  00 00 00 04                                       ....
00000000  00                                                .
00000000  00 00 00 00                                       ....

Class methods

.new(io : IO, output : IO = STDERR, read = false, write = false)

View source

Methods

#close(*args, **options)

View source

#close

View source

#closed?(*args, **options)

View source

#closed?

View source

#flush(*args, **options)

View source

#flush

View source

#peek(*args, **options)

View source

#peek

View source

#pos(*args, **options)

View source

#pos=(arg)

Sets the current position (in bytes) in this IO.

The IO class raises on this method, but some subclasses, notable IO::FileDescriptor and IO::Memory implement it.

File.write("testfile", "hello")

file = File.new("testfile")
file.pos = 3
file.gets_to_end # => "lo"
View source

#read(buf : Bytes)

Reads at most slice.size bytes from this IO into slice. Returns the number of bytes read, which is 0 if and only if there is no more data to read (so checking for 0 is the way to detect end of file).

io = IO::Memory.new "hello"
slice = Bytes.new(4)
io.read(slice) # => 4
slice          # => Bytes[104, 101, 108, 108]
io.read(slice) # => 1
slice          # => Bytes[111, 101, 108, 108]
io.read(slice) # => 0
View source

#seek(*args, **options)

View source

#seek

View source

#tty?

View source

#tty?(*args, **options)

View source

#write(buf : Bytes) : Nil

Writes the contents of slice into this IO.

io = IO::Memory.new
slice = Bytes.new(4) { |i| ('a'.ord + i).to_u8 }
io.write(slice)
io.to_s # => "abcd"
View source