Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/core/hexdump.rb

Overview

hexdump.rb Copyright (C) 2010..2013 Carsten Bormann <[email protected]>

Instance Method Summary collapse

Instance Method Details

#hexdump(prefix = '', prepend_newline = true) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/core/hexdump.rb', line 5

def hexdump(prefix = '', prepend_newline = true)
  a, i = [], 0
  a << '' if prepend_newline
  while i < length
    slice = self.byteslice(i, 16)
    a << '%s%-48s |%-16s|' %
      [prefix,
       slice.bytes.map { |b| '%02x' % b.ord }.join(' '),
       slice.gsub(/[^ -~]/mn, ".")]
    i += 16
  end
  a.join("\n")
end