Module: Net::TNS::StringHelpers

Included in:
String
Defined in:
lib/net/tns/helpers/string_helpers.rb

Overview

This module includes common string helper methods for monkey-patching or mixing-in to string objects.

Constant Summary collapse

HEXCHARS =
[("0".."9").to_a, ("a".."f").to_a].flatten

Instance Method Summary collapse

Instance Method Details

#tns_hexifyObject

Adapted from the Ruby Black Bag (github.com/emonti/rbkb/) Convert a string to ASCII hex string



9
10
11
12
13
# File 'lib/net/tns/helpers/string_helpers.rb', line 9

def tns_hexify
  self.each_byte.map do |byte|
    (HEXCHARS[(byte >> 4)] + HEXCHARS[(byte & 0xf )])
  end.join()
end

#tns_unhexify(d = /\s*/) ⇒ Object

Convert ASCII hex string to raw.

Parameters:

d = optional 'delimiter' between hex bytes (zero+ spaces by default)


20
21
22
# File 'lib/net/tns/helpers/string_helpers.rb', line 20

def tns_unhexify(d=/\s*/)
  self.strip.gsub(/([A-Fa-f0-9]{1,2})#{d}?/) { $1.hex.chr }
end