Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Suuport custom separator
  • Loading branch information
ksss committed Mar 15, 2017
1 parent 6eb02a8 commit 673ce23
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 7 additions & 4 deletions mrblib/string.rb
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/t/string.rb
Expand Up @@ -341,6 +341,12 @@ def to_str
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
Expand Down

0 comments on commit 673ce23

Please sign in to comment.