1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[DOC] Add encoding external/internal example to encoding.rdoc (#5610)

* Add encoding external/internal example to encoding.rdoc

* Add encoding external/internal example to encoding.rdoc

* Update doc/encoding.rdoc

I think there may be some more of these that I've recently put into io.c.  Will check tomorrow and create new PR if so.

Co-authored-by: Peter Zhu <peter@peterzhu.ca>

Co-authored-by: Peter Zhu <peter@peterzhu.ca>
This commit is contained in:
Burdette Lamar 2022-03-01 10:45:18 -06:00 committed by GitHub
parent fb724a887a
commit 7a6ba30c24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2022-03-02 01:45:40 +09:00
Merged-By: BurdetteLamar <BurdetteLamar@Yahoo.com>

View file

@ -274,6 +274,29 @@ For an \IO, \File, \ARGF, or \StringIO object, the internal encoding may be set
- \Method +set_encoding+.
==== Stream \Encoding Example
This example writes a string to a file, encoding it as ISO-8859-1,
then reads the file into a new string, encoding it as UTF-8:
s = "R\u00E9sum\u00E9"
path = 't.tmp'
ext_enc = 'ISO-8859-1'
int_enc = 'UTF-8'
File.write(path, s, external_encoding: ext_enc)
raw_text = File.binread(path)
transcoded_text = File.read(path, external_encoding: ext_enc, internal_encoding: int_enc)
p raw_text
p transcoded_text
Output:
"R\xE9sum\xE9"
"Résumé"
=== Script \Encoding
A Ruby script has a script encoding, which may be retrieved by: