diff --git a/string.c b/string.c index 88516eb7c4..92995d1ce5 100644 --- a/string.c +++ b/string.c @@ -8012,7 +8012,12 @@ rb_str_enumerate_lines(int argc, VALUE *argv, VALUE str, VALUE ary) if (subptr != pend) { if (chomp) { - pend = chomp_newline(subptr, pend, enc); + if (rsnewline) { + pend = chomp_newline(subptr, pend, enc); + } + else if (memcmp(pend - rslen, rsptr, rslen) == 0) { + pend -= rslen; + } } line = rb_str_subseq(str, subptr - ptr, pend - subptr); ENUM_ELEM(ary, line); diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index e88746dc55..b0c6e447eb 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1119,6 +1119,10 @@ CODE res = [] S("\r\n").each_line(chomp: true) {|x| res << x} assert_equal([S("")], res) + + res = [] + S("a\n b\n").each_line(" ", chomp: true) {|x| res << x} + assert_equal([S("a\n"), S("b\n")], res) end def test_lines