diff options
| author | ksss <[email protected]> | 2017-03-15 16:50:27 +0900 |
|---|---|---|
| committer | ksss <[email protected]> | 2017-03-15 16:58:28 +0900 |
| commit | 673ce237c48ed6cd3c516ae6e03835e7b41e278f (patch) | |
| tree | 049ea4a451b8e7483315e4da3bda191a25697ac5 /mrblib/string.rb | |
| parent | 6eb02a892dbd0f0631c0362805fa995194290611 (diff) | |
| download | mruby-673ce237c48ed6cd3c516ae6e03835e7b41e278f.tar.gz mruby-673ce237c48ed6cd3c516ae6e03835e7b41e278f.zip | |
Suuport custom separator
Diffstat (limited to 'mrblib/string.rb')
| -rw-r--r-- | mrblib/string.rb | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/mrblib/string.rb b/mrblib/string.rb index 5510bf6de..f3890dfa7 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -9,12 +9,15 @@ class String # and pass the respective line. # # ISO 15.2.10.5.15 - def each_line(&block) + def each_line(rs = "\n", &block) + return block.call(self) if rs.nil? + rs = rs.to_str offset = 0 + rs_len = rs.length this = dup - while pos = this.index("\n", offset) - block.call(this[offset, pos + 1 - offset]) - offset = pos + 1 + while pos = this.index(rs, offset) + block.call(this[offset, pos + rs_len - offset]) + offset = pos + rs_len end block.call(this[offset, this.size - offset]) if this.size > offset self |
