Skip to content

Instantly share code, notes, and snippets.

@hanabokuro
Last active September 22, 2015 19:09
Show Gist options
  • Save hanabokuro/10104634 to your computer and use it in GitHub Desktop.
Save hanabokuro/10104634 to your computer and use it in GitHub Desktop.
String#undump
class String
def undump
self.sub(/\A"/, '').sub(/"\z/, '').gsub(/\\(x([0-9a-f]{2})) | \\(u\{([0-9a-f]{4})\}) | \\(.) /ix) {
if $1 # \xXX
[$2.hex].pack("C")
elsif $3 # \u{xxxx}
[$4.hex].pack("U")
else # \.
case $5
when 't'
"\t"
when 'v'
"\v"
when 'n'
"\n"
when 'r'
"\r"
when 'f'
"\f"
when 'b'
"\b"
when 'a'
"\a"
when 'e'
"\e"
when 's'
" "
else
$5
end
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment