Skip to content

Instantly share code, notes, and snippets.

@erik-megarad
Created January 11, 2012 23:34
Show Gist options
  • Save erik-megarad/1597477 to your computer and use it in GitHub Desktop.
Save erik-megarad/1597477 to your computer and use it in GitHub Desktop.
For fun, run this at home
["US-ASCII", "ASCII-8BIT", "UTF-8"].each do |encoding|
Encoding.default_internal = encoding
puts "With #{encoding} default internal:"
[127, 255, 256].each do |number|
no_arg = number.chr.encoding rescue "out of range"
puts "#{number}.chr is: #{no_arg}"
with_arg = number.chr(encoding).encoding rescue "out of range"
puts "#{number}.chr('#{encoding}') is: #{with_arg}"
end
puts ""
end
@erik-megarad
Copy link
Author

Spoiler: here's the output:

With US-ASCII default internal:
127.chr is: US-ASCII
127.chr('US-ASCII') is: US-ASCII
255.chr is: ASCII-8BIT
255.chr('US-ASCII') is: US-ASCII
256.chr is: out of range
256.chr('US-ASCII') is: out of range

With ASCII-8BIT default internal:
127.chr is: US-ASCII
127.chr('ASCII-8BIT') is: ASCII-8BIT
255.chr is: ASCII-8BIT
255.chr('ASCII-8BIT') is: ASCII-8BIT
256.chr is: out of range
256.chr('ASCII-8BIT') is: out of range

With UTF-8 default internal:
127.chr is: US-ASCII
127.chr('UTF-8') is: UTF-8
255.chr is: ASCII-8BIT
255.chr('UTF-8') is: UTF-8
256.chr is: UTF-8
256.chr('UTF-8') is: UTF-8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment