Skip to content

Instantly share code, notes, and snippets.

@eric
Created August 25, 2011 01:19
Show Gist options
  • Save eric/1169737 to your computer and use it in GitHub Desktop.
Save eric/1169737 to your computer and use it in GitHub Desktop.
irb(main):036:0> Encoding.default_external
=> #<Encoding:UTF-8>
irb(main):037:0> data
=> "one two three 32\xC2\xB0\n\xEE\x80\x8E\xEE\x80\x97\xEE\x91\x81\xEE\x81\x94\n\xFF"
irb(main):038:0> data.encoding
=> #<Encoding:ASCII-8BIT>
irb(main):039:0> data.valid_encoding?
=> true
irb(main):040:0> encoded_data = data.encode('utf-8', :invalid => :replace, :undef => :replace)
=> "one two three 32��\n������������\n�"
irb(main):041:0> encoded_data
=> "one two three 32��\n������������\n�"
irb(main):042:0> encoded_data.valid_encoding?
=> true
irb(main):043:0> data.force_encoding('utf-8')
=> "one two three 32°\n\n\xFF"
irb(main):044:0> data.valid_encoding?
=> false
irb(main):045:0> data.encoding
=> #<Encoding:UTF-8>
irb(main):046:0> data.encode('utf-8', :invalid => :replace, :undef => :replace)
=> "one two three 32°\n\n\xFF"
irb(main):047:0> data.encode('utf-8', :invalid => :replace, :undef => :replace).valid_encoding?
=> false
irb(main):048:0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment