Introduction

To read a file that uses the UTF-8 encoding scheme:

Demo

File.new("main.rb", "r:utf-8").each { |line| puts line }

Result

Here, we're reading a file in the UTF-8 encoding.

To determine the external encoding of an I/O object using its external_encoding method:

Demo

puts File.open("main.rb", "r:utf-8").external_encoding 
puts File.open("main.rb", "r").external_encoding

Result

If your default encoding is not US-ASCII, the second line will return whatever your default encoding actually is.

Related Topic