Skip to content

Commit 3462ca8

Browse files
committed
[DOC] Doc for StringIO#putc
1 parent e2d24ae commit 3462ca8

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

doc/stringio/putc.rdoc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Replaces one or more bytes at position +pos+
2+
with bytes of the given argument;
3+
returns the argument.
4+
5+
\StringIO object for 1-byte characters.
6+
7+
strio = StringIO.new('foo')
8+
9+
With 1-byte argument, replaces one byte:
10+
11+
strio.putc('b')
12+
strio.string # => "boo"
13+
strio.putc('a') # => "a"
14+
strio.string # => "bao"
15+
strio.putc('r') # => "r"
16+
strio.string # => "bar"
17+
strio.putc('n') # => "n"
18+
strio.string # => "barn"
19+
20+
Fills with null characters if necessary:
21+
22+
strio.pos = 6
23+
strio.putc('x') # => "x"
24+
strio.string # => "barn\u0000\u0000x"
25+
26+
With integer argument, replaces one byte with the low-order byte of the integer:
27+
28+
strio = StringIO.new('foo')
29+
strio.putc(70)
30+
strio.string # => "Foo"
31+
strio.putc(79)
32+
strio.string # => "FOo"
33+
strio.putc(79 + 1024)
34+
strio.string # => "FOO"
35+
36+
\StringIO object for Multi-byte characters:
37+
38+
greek = 'αβγδε' # Five 2-byte characters.
39+
strio = StringIO.new(greek)
40+
strio.string# => "αβγδε"
41+
strio.string.b # => "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5"
42+
strio.string.bytesize # => 10
43+
strio.string.chars # => ["α", "β", "γ", "δ", "ε"]
44+
strio.string.size # => 5
45+
46+
With 1-byte argument, replaces one byte of the string:
47+
48+
strio.putc(' ') # 1-byte ascii space.
49+
strio.string # => " \xB1βγδε"
50+
strio.string.b # => " \xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5"
51+
strio.string.bytesize # => 10
52+
strio.string.chars # => [" ", "\xB1", "β", "γ", "δ", "ε"]
53+
strio.string.size # => 6
54+
55+
strio.putc(' ')
56+
strio.string # => " βγδε"
57+
strio.string.b # => " \xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5"
58+
strio.string.bytesize # => 10
59+
strio.string.chars # => [" ", " ", "β", "γ", "δ", "ε"]
60+
strio.string.size # => 6
61+
62+
With 2-byte argument, replaces two bytes of the string:
63+
64+
strio.rewind
65+
strio.putc('α')
66+
strio.string # => "αβγδε"
67+
strio.string.b # => "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5"
68+
strio.string.bytesize # => 10
69+
strio.string.chars # => ["α", "β", "γ", "δ", "ε"]
70+
strio.string.size # => 5
71+
72+
Related: #getc, #ungetc.

ext/stringio/stringio.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,9 +1615,10 @@ strio_write(VALUE self, VALUE str)
16151615

16161616
/*
16171617
* call-seq:
1618-
* strio.putc(obj) -> obj
1618+
* putc(object) -> object
1619+
*
1620+
* :include: stringio/putc.rdoc
16191621
*
1620-
* See IO#putc.
16211622
*/
16221623
static VALUE
16231624
strio_putc(VALUE self, VALUE ch)

0 commit comments

Comments
 (0)