What is the external_encoding property of a file in Ruby?
Overview
The external_encoding property of a file returns the Encoding object that represents the file's encoding. The Encoding object contains all the encodings available in Ruby.
Syntax
file.external_encoding
Syntax of the external_encoding property
Return value
This property returns the encoding of the file.
Example
Let's look at the example below:
main.rb
test.txt
# create new filef = File.new("test.txt")# print encoding of fileputs f.external_encoding
Explanation
- Line 2: We create a new file,
test.txt. - Line 5: We print the encoding of the file to the console.