string.c: chomp option

* string.c (rb_str_enumerate_lines): implement chomp option.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56972 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-12-03 14:18:03 +00:00
parent 9fa8006a01
commit d95f5bc81a
2 changed files with 76 additions and 6 deletions

View file

@ -816,6 +816,35 @@ CODE
$/ = save
end
def test_each_line_chomp
res = []
S("hello\nworld").each_line("\n", chomp: true) {|x| res << x}
assert_equal(S("hello"), res[0])
assert_equal(S("world"), res[1])
res = []
S("hello\n\n\nworld").each_line(S(''), chomp: true) {|x| res << x}
assert_equal(S("hello\n\n"), res[0])
assert_equal(S("world"), res[1])
res = []
S("hello!world").each_line(S('!'), chomp: true) {|x| res << x}
assert_equal(S("hello"), res[0])
assert_equal(S("world"), res[1])
res = []
S("a").each_line(S('ab'), chomp: true).each {|x| res << x}
assert_equal(1, res.size)
assert_equal(S("a"), res[0])
s = nil
"foo\nbar".each_line(nil, chomp: true) {|s2| s = s2 }
assert_equal("foo\nbar", s)
assert_equal "hello", S("hello\nworld").each_line(chomp: true).next
assert_equal "hello\nworld", S("hello\nworld").each_line(nil, chomp: true).next
end
def test_lines
s = S("hello\nworld")
assert_equal ["hello\n", "world"], s.lines