Skip to content
Merged
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ The following default gems are updated.

* RubyGems 4.0.0.dev
* bundler 4.0.0.dev
* erb 5.1.1
* erb 5.1.3
* etc 1.4.6
* fcntl 1.3.0
* io-console 0.8.1
Expand Down
4 changes: 3 additions & 1 deletion doc/string/sum.rdoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Returns a basic +n+-bit checksum of the characters in +self+;
Returns a basic +n+-bit {checksum}[https://en.wikipedia.org/wiki/Checksum] of the characters in +self+;
the checksum is the sum of the binary value of each byte in +self+,
modulo <tt>2**n - 1</tt>:

Expand All @@ -9,3 +9,5 @@ modulo <tt>2**n - 1</tt>:
'こんにちは'.sum # => 2582

This is not a particularly strong checksum.

Related: see {Querying}[rdoc-ref:String@Querying].
19 changes: 19 additions & 0 deletions doc/string/swapcase.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Returns a string containing the characters in +self+, with cases reversed:

- Each uppercase character is downcased.
- Each lowercase character is upcased.

Examples:

'Hello World!'.swapcase # => "hELLO wORLD!"
'тест'.swapcase # => "ТЕСТ"

Some characters (and even character sets) do not have casing:

'12345'.swapcase # => "12345"
'こんにちは'.swapcase # => "こんにちは"

The casing may be affected by the given +mapping+;
see {Case Mapping}[rdoc-ref:case_mapping.rdoc].

Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].
34 changes: 34 additions & 0 deletions doc/stringio/each_byte.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
With a block given, calls the block with each remaining byte in the stream;
positions the stream at end-of-file;
returns +self+:

bytes = []
strio = StringIO.new('hello') # Five 1-byte characters.
strio.each_byte {|byte| bytes.push(byte) }
strio.eof? # => true
bytes # => [104, 101, 108, 108, 111]
bytes = []
strio = StringIO.new('тест') # Four 2-byte characters.
strio.each_byte {|byte| bytes.push(byte) }
bytes # => [209, 130, 208, 181, 209, 129, 209, 130]
bytes = []
strio = StringIO.new('こんにちは') # Five 3-byte characters.
strio.each_byte {|byte| bytes.push(byte) }
bytes # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]

The position in the stream matters:

bytes = []
strio = StringIO.new('こんにちは')
strio.getc # => "こ"
strio.pos # => 3 # 3-byte character was read.
strio.each_byte {|byte| bytes.push(byte) }
bytes # => [227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]

If at end-of-file, does not call the block:

strio.eof? # => true
strio.each_byte {|byte| fail 'Boo!' }
strio.eof? # => true

With no block given, returns a new {Enumerator}[rdoc-ref:Enumerator].
34 changes: 34 additions & 0 deletions doc/stringio/each_char.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
With a block given, calls the block with each remaining character in the stream;
positions the stream at end-of-file;
returns +self+:

chars = []
strio = StringIO.new('hello')
strio.each_char {|char| chars.push(char) }
strio.eof? # => true
chars # => ["h", "e", "l", "l", "o"]
chars = []
strio = StringIO.new('тест')
strio.each_char {|char| chars.push(char) }
chars # => ["т", "е", "с", "т"]
chars = []
strio = StringIO.new('こんにちは')
strio.each_char {|char| chars.push(char) }
chars # => ["こ", "ん", "に", "ち", "は"]

Stream position matters:

chars = []
strio = StringIO.new('こんにちは')
strio.getc # => "こ"
strio.pos # => 3 # 3-byte character was read.
strio.each_char {|char| chars.push(char) }
chars # => ["ん", "に", "ち", "は"]

When at end-of-stream does not call the block:

strio.eof? # => true
strio.each_char {|char| fail 'Boo!' }
strio.eof? # => true

With no block given, returns a new {Enumerator}[rdoc-ref:Enumerator].
36 changes: 36 additions & 0 deletions doc/stringio/each_codepoint.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
With a block given, calls the block with each successive codepoint from self;
sets the position to end-of-stream;
returns +self+.

Each codepoint is the integer value for a character; returns self:

codepoints = []
strio = StringIO.new('hello')
strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
strio.eof? # => true
codepoints # => [104, 101, 108, 108, 111]
codepoints = []
strio = StringIO.new('тест')
strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
codepoints # => [1090, 1077, 1089, 1090]
codepoints = []
strio = StringIO.new('こんにちは')
strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
codepoints # => [12371, 12435, 12395, 12385, 12399]

Position in the stream matters:

codepoints = []
strio = StringIO.new('こんにちは')
strio.getc # => "こ"
strio.pos # => 3
strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
codepoints # => [12435, 12395, 12385, 12399]

When at end-of-stream, the block is not called:

strio.eof? # => true
strio.each_codepoint {|codepoint| fail 'Boo!' }
strio.eof? # => true

With no block given, returns a new {Enumerator}[rdoc-ref:Enumerator].
19 changes: 8 additions & 11 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,9 @@ strio_get_sync(VALUE self)
* call-seq:
* each_byte {|byte| ... } -> self
*
* With a block given, calls the block with each remaining byte in the stream;
* see {Byte IO}[rdoc-ref:IO@Byte+IO].
* :include: stringio/each_byte.rdoc
*
* With no block given, returns an enumerator.
* Related: StringIO#each_char, StringIO#each_codepoint, StringIO#each_line.
*/
static VALUE
strio_each_byte(VALUE self)
Expand Down Expand Up @@ -1162,12 +1161,11 @@ strio_readbyte(VALUE self)

/*
* call-seq:
* each_char {|c| ... } -> self
* each_char {|char| ... } -> self
*
* With a block given, calls the block with each remaining character in the stream;
* see {Character IO}[rdoc-ref:IO@Character+IO].
* :include: stringio/each_char.rdoc
*
* With no block given, returns an enumerator.
* Related: StringIO#each_byte, StringIO#each_codepoint, StringIO#each_line.
*/
static VALUE
strio_each_char(VALUE self)
Expand All @@ -1186,10 +1184,9 @@ strio_each_char(VALUE self)
* call-seq:
* each_codepoint {|codepoint| ... } -> self
*
* With a block given, calls the block with each remaining codepoint in the stream;
* see {Codepoint IO}[rdoc-ref:IO@Codepoint+IO].
* :include: stringio/each_codepoint.rdoc
*
* With no block given, returns an enumerator.
* Related: StringIO#each_byte, StringIO#each_char, StringIO#each_line.
*/
static VALUE
strio_each_codepoint(VALUE self)
Expand Down Expand Up @@ -1630,7 +1627,7 @@ strio_readline(int argc, VALUE *argv, VALUE self)
* "Fifth line"
* ```
*
* With no block given, returns a new {Enumerator}[rdoc-ref:Enumerator].
* With no block given, returns a new {Enumerator}[rdoc-ref:Enumerator].
*
* Related: StringIO.each_byte, StringIO.each_char, StringIO.each_codepoint.
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/erb/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
class ERB
# The string \ERB version.
VERSION = '5.1.1'
VERSION = '5.1.3'
end
32 changes: 7 additions & 25 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -6190,7 +6190,7 @@ rb_pat_search(VALUE pat, VALUE str, long pos, int set_backref_str)
*
* Like String#sub, except that:
*
* - Changed are made to +self+, not to copy of +self+.
* - Changes are made to +self+, not to copy of +self+.
* - Returns +self+ if any changes are made, +nil+ otherwise.
*
* Related: see {Modifying}[rdoc-ref:String@Modifying].
Expand Down Expand Up @@ -8203,20 +8203,12 @@ rb_str_capitalize(int argc, VALUE *argv, VALUE str)
* call-seq:
* swapcase!(mapping) -> self or nil
*
* Upcases each lowercase character in +self+;
* downcases uppercase character;
* returns +self+ if any changes were made, +nil+ otherwise:
*
* s = 'Hello World!' # => "Hello World!"
* s.swapcase! # => "hELLO wORLD!"
* s # => "hELLO wORLD!"
* ''.swapcase! # => nil
* Like String#swapcase, except that:
*
* The casing may be affected by the given +mapping+;
* see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
*
* Related: String#swapcase.
* - Changes are made to +self+, not to copy of +self+.
* - Returns +self+ if any changes are made, +nil+ otherwise.
*
* Related: see {Modifying}[rdoc-ref:String@Modifying].
*/

static VALUE
Expand All @@ -8240,19 +8232,9 @@ rb_str_swapcase_bang(int argc, VALUE *argv, VALUE str)

/*
* call-seq:
* swapcase(mapping) -> string
*
* Returns a string containing the characters in +self+, with cases reversed;
* each uppercase character is downcased;
* each lowercase character is upcased:
*
* s = 'Hello World!' # => "Hello World!"
* s.swapcase # => "hELLO wORLD!"
*
* The casing may be affected by the given +mapping+;
* see {Case Mapping}[rdoc-ref:case_mapping.rdoc].
* swapcase(mapping) -> new_string
*
* Related: String#swapcase!.
* :include: doc/string/swapcase.rdoc
*
*/

Expand Down