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:
parent
fb724a887a
commit
7a6ba30c24
Notes:
git
2022-03-02 01:45:40 +09:00
Merged-By: BurdetteLamar <BurdetteLamar@Yahoo.com>
1 changed files with 23 additions and 0 deletions
|
@ -274,6 +274,29 @@ For an \IO, \File, \ARGF, or \StringIO object, the internal encoding may be set
|
||||||
|
|
||||||
- \Method +set_encoding+.
|
- \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
|
=== Script \Encoding
|
||||||
|
|
||||||
A Ruby script has a script encoding, which may be retrieved by:
|
A Ruby script has a script encoding, which may be retrieved by:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue