diff options
author | Burdette Lamar <[email protected]> | 2025-05-14 14:24:30 -0500 |
---|---|---|
committer | GitHub <[email protected]> | 2025-05-14 15:24:30 -0400 |
commit | 7afee53fa068918a03d5b7465a53c5552234a782 (patch) | |
tree | f590ce13bf6174701561a527491746793a6db1fe /string.c | |
parent | 10e8119cff10cc9b74cd9d69f9dbab6d61702211 (diff) |
[DOC] Tweaks for String#<< (#13306)
Notes
Notes:
Merged-By: peterzhu2118 <[email protected]>
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 33 |
1 files changed, 19 insertions, 14 deletions
@@ -4308,22 +4308,34 @@ rb_str_append_as_bytes(int argc, VALUE *argv, VALUE str) /* * call-seq: - * string << object -> string + * self << object -> self * - * Concatenates +object+ to +self+ and returns +self+: + * Appends a string representation of +object+ to +self+; + * returns +self+. + * + * If +object+ is a string, appends it to +self+: * * s = 'foo' * s << 'bar' # => "foobar" * s # => "foobar" * - * If +object+ is an Integer, - * the value is considered a codepoint and converted to a character before concatenation: + * If +object+ is an integer, + * its value is considered a codepoint; + * converts the value to a character before concatenating: * * s = 'foo' * s << 33 # => "foo!" * - * If that codepoint is not representable in the encoding of - * _string_, RangeError is raised. + * Additionally, if the codepoint is in range <tt>0..0xff</tt> + * and the encoding of +self+ is Encoding::US_ASCII, + * changes the encoding to Encoding::ASCII_8BIT: + * + * s = 'foo'.encode(Encoding::US_ASCII) + * s.encoding # => #<Encoding:US-ASCII> + * s << 0xff # => "foo\xFF" + * s.encoding # => #<Encoding:BINARY (ASCII-8BIT)> + * + * Raises RangeError if that codepoint is not representable in the encoding of +self+: * * s = 'foo' * s.encoding # => <Encoding:UTF-8> @@ -4331,14 +4343,7 @@ rb_str_append_as_bytes(int argc, VALUE *argv, VALUE str) * s = 'foo'.encode(Encoding::EUC_JP) * s << 0x00800080 # invalid codepoint 0x800080 in EUC-JP (RangeError) * - * If the encoding is US-ASCII and the codepoint is 0..0xff, _string_ - * is automatically promoted to ASCII-8BIT. - * - * s = 'foo'.encode(Encoding::US_ASCII) - * s << 0xff - * s.encoding # => #<Encoding:BINARY (ASCII-8BIT)> - * - * Related: String#concat, which takes multiple arguments. + * Related: see {Modifying}[rdoc-ref:String@Modifying]. */ VALUE rb_str_concat(VALUE str1, VALUE str2) |