Skip to content

Instantly share code, notes, and snippets.

@backus
Created July 20, 2016 19:37
Show Gist options
  • Save backus/b9b4c3df157041eeb2bf03adb357482c to your computer and use it in GitHub Desktop.
Save backus/b9b4c3df157041eeb2bf03adb357482c to your computer and use it in GitHub Desktop.
gem 'json', '= 2.0.1'
require 'json'
JSON::VERSION # => "2.0.1"
json_string = '{}'
utf8_string = json_string.dup.force_encoding('UTF-8')
utf8_string_frozen = utf8_string.dup.freeze
ascii8_string = json_string.dup.force_encoding('ASCII-8BIT')
ascii8_string_frozen = ascii8_string.dup.freeze
utf8_string.encoding # => #<Encoding:UTF-8>
utf8_string_frozen.encoding # => #<Encoding:UTF-8>
ascii8_string.encoding # => #<Encoding:ASCII-8BIT>
ascii8_string_frozen.encoding # => #<Encoding:ASCII-8BIT>
utf8_string.frozen? # => false
utf8_string_frozen.frozen? # => true
ascii8_string.frozen? # => false
ascii8_string_frozen.frozen? # => true
JSON.parse(utf8_string) # => {}
JSON.parse(utf8_string_frozen) # => {}
JSON.parse(ascii8_string) # => {}
JSON.parse(ascii8_string_frozen) # can't modify frozen String (RuntimeError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment