Skip to content

Instantly share code, notes, and snippets.

@aruprakshit
Forked from rklemme/encoding-line-break-test.rb
Last active August 29, 2015 14:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aruprakshit/4df089594c5761e1e7da to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby -w
FILE_NAME = "x"
printf "%-20s %p\n%-20s %p\n",
"Default external", Encoding.default_external,
"Default internal", Encoding.default_internal
# p File.instance_methods.grep(/enc|opt/)
def show(line)
printf "%-20s %p\n", line.encoding, line
end
# prepare
File.open(FILE_NAME, "w", encoding: "BINARY") {|io| io.write("1\n2\r3\rn4\n") }
puts
File.open FILE_NAME, universal_newline: true do |io|
printf "%-20s %p\n%-20s %p\n",
"external", io.external_encoding,
"internal", io.internal_encoding
io.each_line do |line|
show line
end
end
puts
File.open FILE_NAME, internal_encoding: Encoding.default_external, universal_newline: true do |io|
printf "%-20s %p\n%-20s %p\n",
"external", io.external_encoding,
"internal", io.internal_encoding
io.each_line do |line|
show line
end
end
puts
File.open FILE_NAME, internal_encoding: "ISO-8859-1", universal_newline: true do |io|
printf "%-20s %p\n%-20s %p\n",
"external", io.external_encoding,
"internal", io.internal_encoding
io.each_line do |line|
show line
end
end
$ ./encoding-line-break-test.rb
Default external #<Encoding:UTF-8>
Default internal nil
external #<Encoding:UTF-8>
internal nil
UTF-8 "1\n"
UTF-8 "2\r3\rn4\n"
external #<Encoding:UTF-8>
internal nil
UTF-8 "1\n"
UTF-8 "2\r3\rn4\n"
external #<Encoding:UTF-8>
internal #<Encoding:ISO-8859-1>
ISO-8859-1 "1\n"
ISO-8859-1 "2\n"
ISO-8859-1 "3\n"
ISO-8859-1 "n4\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment