|
| 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. |
0 commit comments