Skip to content

Instantly share code, notes, and snippets.

@matthauck
Created January 18, 2013 10:25
Show Gist options
  • Save matthauck/4563666 to your computer and use it in GitHub Desktop.
Save matthauck/4563666 to your computer and use it in GitHub Desktop.
utf-8 broken in jruby 1.7.2 windows
# encoding: utf-8
require 'tempfile'
def valid_utf8(str)
str.encoding == Encoding::UTF_8 && str.valid_encoding?
end
str = <<-EOF
My UTF-8 String: ✓ ®
EOF
puts str
puts "Original valid utf-8? #{valid_utf8(str)}"
file = Tempfile.new('temp')
path = file.path
file.write(str)
file.rewind
read = file.read
puts "Read #1 valid? #{valid_utf8(read)}"
file.close
read = File.read(path)
puts "Read #2 valid? #{valid_utf8(read)}"
read = File.read(path, :encoding => 'utf-8')
puts "Read #3 valid? #{valid_utf8(read)}"
read = File.open(path, 'r:utf-8') {|f| f.read}
puts "Read #4 valid? #{valid_utf8(read)}"
File.unlink path
File.open(path, 'w:utf-8'){|f| f.write str }
read = File.read(path)
puts "Read #5 valid? #{valid_utf8(read)}"
read = File.read(path, :encoding => 'utf-8')
puts "Read #6 valid? #{valid_utf8(read)}"
read = File.open(path, 'r:utf-8'){|f| f.read }
puts "Read #7 valid? #{valid_utf8(read)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment