Skip to content

Instantly share code, notes, and snippets.

@RX14

RX14/code.cr Secret

Created February 14, 2017 00:50
Show Gist options
  • Save RX14/e2dd192c7cb0e7cfb2278c2c46f3bea3 to your computer and use it in GitHub Desktop.
Save RX14/e2dd192c7cb0e7cfb2278c2c46f3bea3 to your computer and use it in GitHub Desktop.
pp string
string.each_byte do |byte|
p byte <= 0x1F
case byte
when '\t'.ord, ' '.ord, '"'.ord, '\\'.ord
io << '\\'
when byte <= 0x1F, 0x7F
raise ArgumentError.new("String contained invalid character #{byte.chr.inspect}")
end
io.write_byte byte
end
string # => "foo\u{0}bar"
false
false
false
true
false
false
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment