diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-03-16 22:18:15 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-03-16 22:18:15 +0900 |
| commit | 9ecebebf3e12d6d8125a7a24b8ac4e124c7cd98a (patch) | |
| tree | 079527e7703b3408c9575bc7249905e46d199c67 | |
| parent | 47cc5d66e3a2b2b53f7c172aaf4c38baee162556 (diff) | |
| parent | 4ee79a877728f07c2017ba0a225bf9647bfca22a (diff) | |
| download | mruby-9ecebebf3e12d6d8125a7a24b8ac4e124c7cd98a.tar.gz mruby-9ecebebf3e12d6d8125a7a24b8ac4e124c7cd98a.zip | |
Merge pull request #3510 from ksss/string-each_line
Some update for `String#each_line`
| -rw-r--r-- | mrblib/string.rb | 15 | ||||
| -rw-r--r-- | test/t/string.rb | 6 |
2 files changed, 16 insertions, 5 deletions
diff --git a/mrblib/string.rb b/mrblib/string.rb index 7add360a8..6e55ee341 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -9,13 +9,18 @@ class String # and pass the respective line. # # ISO 15.2.10.5.15 - def each_line(&block) + def each_line(rs = "\n", &block) + return to_enum(:each_line, rs, &block) unless block + return block.call(self) if rs.nil? + rs = rs.to_str offset = 0 - while pos = self.index("\n", offset) - block.call(self[offset, pos + 1 - offset]) - offset = pos + 1 + rs_len = rs.length + this = dup + while pos = this.index(rs, offset) + block.call(this[offset, pos + rs_len - offset]) + offset = pos + rs_len end - block.call(self[offset, self.size - offset]) if self.size > offset + block.call(this[offset, this.size - offset]) if this.size > offset self end diff --git a/test/t/string.rb b/test/t/string.rb index 25c599ad4..ddc74fa54 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -341,6 +341,12 @@ assert('String#each_line', '15.2.10.5.15') do end assert_equal list, n_list + + n_list.clear + a.each_line("li") do |line| + n_list << line + end + assert_equal ["first li", "ne\nsecond li", "ne\nthird li", "ne"], n_list end assert('String#empty?', '15.2.10.5.16') do |
