diff options
author | Burdette Lamar <BurdetteLamar@Yahoo.com> | 2022-03-01 10:45:18 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-01 10:45:18 -0600 |
commit | 7a6ba30c24f2d796f93baed5d772b56680d042e6 (patch) | |
tree | 7d84dbd1a2a30be8fae6378d076d57d59fd7f41f | |
parent | fb724a887aa239de2fb1920f769ea097bee37b01 (diff) | |
download | ruby-7a6ba30c24f2d796f93baed5d772b56680d042e6.tar.gz |
[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>
-rw-r--r-- | doc/encoding.rdoc | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/doc/encoding.rdoc b/doc/encoding.rdoc index 3c6d1f2889..e09cfee898 100644 --- a/doc/encoding.rdoc +++ b/doc/encoding.rdoc @@ -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: |