[DOC] Tweaks for String#<< (#13306)

This commit is contained in:
Burdette Lamar 2025-05-14 14:24:30 -05:00 committed by GitHub
parent 10e8119cff
commit 7afee53fa0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
Notes: git 2025-05-14 19:24:43 +00:00
Merged-By: peterzhu2118 <peter@peterzhu.ca>

View File

@ -4308,22 +4308,34 @@ rb_str_append_as_bytes(int argc, VALUE *argv, VALUE str)
/* /*
* call-seq: * 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 = 'foo'
* s << 'bar' # => "foobar" * s << 'bar' # => "foobar"
* s # => "foobar" * s # => "foobar"
* *
* If +object+ is an Integer, * If +object+ is an integer,
* the value is considered a codepoint and converted to a character before concatenation: * its value is considered a codepoint;
* converts the value to a character before concatenating:
* *
* s = 'foo' * s = 'foo'
* s << 33 # => "foo!" * s << 33 # => "foo!"
* *
* If that codepoint is not representable in the encoding of * Additionally, if the codepoint is in range <tt>0..0xff</tt>
* _string_, RangeError is raised. * 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 = 'foo'
* s.encoding # => <Encoding:UTF-8> * 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 = 'foo'.encode(Encoding::EUC_JP)
* s << 0x00800080 # invalid codepoint 0x800080 in EUC-JP (RangeError) * s << 0x00800080 # invalid codepoint 0x800080 in EUC-JP (RangeError)
* *
* If the encoding is US-ASCII and the codepoint is 0..0xff, _string_ * Related: see {Modifying}[rdoc-ref:String@Modifying].
* 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.
*/ */
VALUE VALUE
rb_str_concat(VALUE str1, VALUE str2) rb_str_concat(VALUE str1, VALUE str2)