Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO::Buffer#hexdump
() -> ::String
hexdump([offset, [length, [width]]]) → string or nil
io_buffer.c
View on GitHub ↗
static VALUE
rb_io_buffer_hexdump(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 3);
size_t offset, length;
struct rb_io_buffer_view *buffer = io_buffer_extract_offset_length(self, argc, argv, &offset, &length);
size_t width = RB_IO_BUFFER_HEXDUMP_DEFAULT_WIDTH;
if (argc >= 3) {
width = io_buffer_extract_width(argv[2], 1);
}
// This may raise an exception if the offset/length is invalid:
io_buffer_validate_range(buffer, offset, length);
VALUE result = Qnil;
void *base = NULL;
size_t size = 0;
if (io_buffer_try_get_bytes(buffer, &base, &size) && base) {
result = rb_str_buf_new(io_buffer_hexdump_output_size(width, length, 1));
io_buffer_hexdump(result, width, base, offset+length, offset, 1);
}
return result;
}
Returns a human-readable string representation of the buffer. The exact format is subject to change.
Returns nil if the buffer does not reference any memory, that is, if null? returns true (for example after free or transfer).
Example
buffer = IO::Buffer.for("Hello World") puts buffer.hexdump # 0x00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 Hello World
As buffers are usually fairly big, you may want to limit the output by specifying the offset and length:
Example
puts buffer.hexdump(6, 5) # 0x00000006 57 6f 72 6c 64 World