Skip to content

Commit 8983f32

Browse files
[DOC] Doc for StringIO#putc (#196)
Previous doc merely linked to `IO#putc`. The new doc stays local, provides examples using `StringIO` objects.
1 parent 806f3d9 commit 8983f32

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

doc/stringio/putc.rdoc

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