Created
March 23, 2012 22:56
-
-
Save lpar/2176075 to your computer and use it in GitHub Desktop.
Brief demonstration of using character encodings in Ruby 1.9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mini-esta��o de demonstra��o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment