Skip to content

Commit dd8903f

Browse files
committed
[Bug #20566] Mention out-of-range argument cases in String#<<
Also [Bug #18973].
1 parent 6ea9cd4 commit dd8903f

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

doc/format_specifications.rdoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ Format +argument+ as a single character:
233233
sprintf('%c', 'A') # => "A"
234234
sprintf('%c', 65) # => "A"
235235

236+
This behaves like String#<<, except for raising ArgumentError instead of RangeError.
237+
236238
=== Specifier +d+
237239

238240
Format +argument+ as a decimal integer:

string.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,6 +3595,22 @@ rb_str_concat_multi(int argc, VALUE *argv, VALUE str)
35953595
* s = 'foo'
35963596
* s << 33 # => "foo!"
35973597
*
3598+
* If that codepoint is not representable in the encoding of
3599+
* _string_, RangeError is raised.
3600+
*
3601+
* s = 'foo'
3602+
* s.encoding # => <Encoding:UTF-8>
3603+
* s << 0x00110000 # 1114112 out of char range (RangeError)
3604+
* s = 'foo'.encode('EUC-JP')
3605+
* s << 0x00800080 # invalid codepoint 0x800080 in EUC-JP (RangeError)
3606+
*
3607+
* If the encoding is US-ASCII and the codepoint is 0..0xff, _string_
3608+
* is automatically promoted to ASCII-8BIT.
3609+
*
3610+
* s = 'foo'.encode('US-ASCII')
3611+
* s << 0xff
3612+
* s.encoding # => #<Encoding:BINARY (ASCII-8BIT)>
3613+
*
35983614
* Related: String#concat, which takes multiple arguments.
35993615
*/
36003616
VALUE

0 commit comments

Comments
 (0)