Skip to content

Commit dedde99

Browse files
[DOC] Use Japanese for multi-byte characters (ruby#15745)
1 parent 7b3b1a1 commit dedde99

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

doc/examples/files.rdoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ text = <<~EOT
77
Fifth line
88
EOT
99

10-
# Russian text.
11-
russian = "\u{442 435 441 442}" # => "тест"
10+
# Japanese text.
11+
japanese = 'こんにちは'
1212

1313
# Binary data.
1414
data = "\u9990\u9991\u9992\u9993\u9994"
1515

1616
# Text file.
1717
File.write('t.txt', text)
1818

19-
# File with Russian text.
20-
File.write('t.rus', russian)
19+
# File with Japanese text.
20+
File.write('t.ja', japanese)
2121

2222
# File with binary data.
2323
f = File.new('t.dat', 'wb:UTF-16')

io.c

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4720,10 +4720,11 @@ rb_io_each_line(int argc, VALUE *argv, VALUE io)
47204720
* Calls the given block with each byte (0..255) in the stream; returns +self+.
47214721
* See {Byte IO}[rdoc-ref:IO@Byte+IO].
47224722
*
4723-
* f = File.new('t.rus')
4723+
* File.read('t.ja') # => "こんにちは"
4724+
* f = File.new('t.ja')
47244725
* a = []
47254726
* f.each_byte {|b| a << b }
4726-
* a # => [209, 130, 208, 181, 209, 129, 209, 130]
4727+
* a # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
47274728
* f.close
47284729
*
47294730
* Returns an Enumerator if no block is given.
@@ -4868,10 +4869,11 @@ io_getc(rb_io_t *fptr, rb_encoding *enc)
48684869
* Calls the given block with each character in the stream; returns +self+.
48694870
* See {Character IO}[rdoc-ref:IO@Character+IO].
48704871
*
4871-
* f = File.new('t.rus')
4872+
* File.read('t.ja') # => "こんにちは"
4873+
* f = File.new('t.ja')
48724874
* a = []
48734875
* f.each_char {|c| a << c.ord }
4874-
* a # => [1090, 1077, 1089, 1090]
4876+
* a # => [12371, 12435, 12395, 12385, 12399]
48754877
* f.close
48764878
*
48774879
* Returns an Enumerator if no block is given.
@@ -4906,10 +4908,11 @@ rb_io_each_char(VALUE io)
49064908
*
49074909
* Calls the given block with each codepoint in the stream; returns +self+:
49084910
*
4909-
* f = File.new('t.rus')
4911+
* File.read('t.ja') # => "こんにちは"
4912+
* f = File.new('t.ja')
49104913
* a = []
49114914
* f.each_codepoint {|c| a << c }
4912-
* a # => [1090, 1077, 1089, 1090]
4915+
* a # => [12371, 12435, 12395, 12385, 12399]
49134916
* f.close
49144917
*
49154918
* Returns an Enumerator if no block is given.
@@ -5023,8 +5026,9 @@ rb_io_each_codepoint(VALUE io)
50235026
* f = File.open('t.txt')
50245027
* f.getc # => "F"
50255028
* f.close
5026-
* f = File.open('t.rus')
5027-
* f.getc.ord # => 1090
5029+
* File.read('t.ja') # => "こんにちは"
5030+
* f = File.open('t.ja')
5031+
* f.getc.ord # => 12371
50285032
* f.close
50295033
*
50305034
* Related: IO#readchar (may raise EOFError).
@@ -5056,8 +5060,9 @@ rb_io_getc(VALUE io)
50565060
* f = File.open('t.txt')
50575061
* f.readchar # => "F"
50585062
* f.close
5059-
* f = File.open('t.rus')
5060-
* f.readchar.ord # => 1090
5063+
* File.read('t.ja') # => "こんにちは"
5064+
* f = File.open('t.ja')
5065+
* f.readchar.ord # => 12371
50615066
* f.close
50625067
*
50635068
* Related: IO#getc (will not raise EOFError).
@@ -5086,8 +5091,9 @@ rb_io_readchar(VALUE io)
50865091
* f = File.open('t.txt')
50875092
* f.getbyte # => 70
50885093
* f.close
5089-
* f = File.open('t.rus')
5090-
* f.getbyte # => 209
5094+
* File.read('t.ja') # => "こんにちは"
5095+
* f = File.open('t.ja')
5096+
* f.getbyte # => 227
50915097
* f.close
50925098
*
50935099
* Related: IO#readbyte (may raise EOFError).
@@ -5130,8 +5136,9 @@ rb_io_getbyte(VALUE io)
51305136
* f = File.open('t.txt')
51315137
* f.readbyte # => 70
51325138
* f.close
5133-
* f = File.open('t.rus')
5134-
* f.readbyte # => 209
5139+
* File.read('t.ja') # => "こんにちは"
5140+
* f = File.open('t.ja')
5141+
* f.readbyte # => 227
51355142
* f.close
51365143
*
51375144
* Related: IO#getbyte (will not raise EOFError).
@@ -9492,7 +9499,8 @@ static VALUE io_initialize(VALUE io, VALUE fnum, VALUE vmode, VALUE opt);
94929499
* The new \IO object does not inherit encoding
94939500
* (because the integer file descriptor does not have an encoding):
94949501
*
9495-
* fd = IO.sysopen('t.rus', 'rb')
9502+
* File.read('t.ja') # => "こんにちは"
9503+
* fd = IO.sysopen('t.ja', 'rb')
94969504
* io = IO.new(fd)
94979505
* io.external_encoding # => #<Encoding:UTF-8> # Not ASCII-8BIT.
94989506
*
@@ -15304,11 +15312,13 @@ set_LAST_READ_LINE(VALUE val, ID _x, VALUE *_y)
1530415312
* File.open('t.txt') {|f| f.gets(11) } # => "First line\n"
1530515313
* File.open('t.txt') {|f| f.gets(12) } # => "First line\n"
1530615314
*
15307-
* # Text with 2-byte characters, which will not be split.
15308-
* File.open('t.rus') {|f| f.gets(1).size } # => 1
15309-
* File.open('t.rus') {|f| f.gets(2).size } # => 1
15310-
* File.open('t.rus') {|f| f.gets(3).size } # => 2
15311-
* File.open('t.rus') {|f| f.gets(4).size } # => 2
15315+
* # Text with 3-byte characters, which will not be split.
15316+
* File.read('t.ja') # => "こんにちは"
15317+
* File.open('t.ja') {|f| f.gets(1).size } # => 1
15318+
* File.open('t.ja') {|f| f.gets(2).size } # => 1
15319+
* File.open('t.ja') {|f| f.gets(3).size } # => 1
15320+
* File.open('t.ja') {|f| f.gets(4).size } # => 2
15321+
* File.open('t.ja') {|f| f.gets(5).size } # => 2
1531215322
*
1531315323
* ===== Line Separator and Line Limit
1531415324
*

0 commit comments

Comments
 (0)