Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # encoding: UTF-8
- # Notice I set UTF-8 as the default above. As far as I'm concerned there
- # are two choices of encoding: UTF-8, and legacy crap. If I need to deal
- # with anything else I'll handle it explicitly, as in this example.
- # Open an ISO-8859-1 file.
- infile = File.open("iso88591.txt", "r:iso-8859-1")
- # Read a line from the file. Even though we've specified a default encoding
- # in this script, the line we read in stays in the encoding specified when
- # the file was opened.
- line = infile.gets
- puts "Line of text in original encoding:"
- puts line
- # Line shows up as garbage in my Linux terminal, which is UTF-8.
- # Now convert it to UTF-8 and redisplay it
- uline = line.encode("UTF-8")
- puts "Line of text in UTF-8:"
- puts uline
- # This time it shows up correctly.
- # Now we'll write out to a new file.
- outfile = File.open("utf8.txt", "w:UTF-8")
- outfile.puts line
- # Ruby automatically converts the ISO-8859-1 string
- # to UTF-8 before writing to the file.
- outfile.close
- infile.close
Add Comment
Please, Sign In to add comment