From 7afee53fa068918a03d5b7465a53c5552234a782 Mon Sep 17 00:00:00 2001 From: Burdette Lamar Date: Wed, 14 May 2025 14:24:30 -0500 Subject: [PATCH] [DOC] Tweaks for String#<< (#13306) --- string.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/string.c b/string.c index 0d985904b0..a1c0137880 100644 --- a/string.c +++ b/string.c @@ -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 0..0xff + * and the encoding of +self+ is Encoding::US_ASCII, + * changes the encoding to Encoding::ASCII_8BIT: + * + * s = 'foo'.encode(Encoding::US_ASCII) + * s.encoding # => # + * s << 0xff # => "foo\xFF" + * s.encoding # => # + * + * Raises RangeError if that codepoint is not representable in the encoding of +self+: * * s = 'foo' * s.encoding # => @@ -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 # => # - * - * Related: String#concat, which takes multiple arguments. + * Related: see {Modifying}[rdoc-ref:String@Modifying]. */ VALUE rb_str_concat(VALUE str1, VALUE str2)