1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Escape skip_lines string before convert to Regexp.

It ignored all of lines when given Regexp special characters.

  [Feature #9147][ruby-core:58549]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59639 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
hsbt 2017-08-22 05:32:34 +00:00
parent 84bc175cab
commit 2c69f7b278
2 changed files with 10 additions and 1 deletions

View file

@ -2159,7 +2159,7 @@ class CSV
# See also CSV.new
def init_comments(skip_lines)
@skip_lines = skip_lines
@skip_lines = Regexp.new(@skip_lines) if @skip_lines.is_a? String
@skip_lines = Regexp.new(Regexp.escape(@skip_lines)) if @skip_lines.is_a? String
if @skip_lines and not @skip_lines.respond_to?(:match)
raise ArgumentError, ":skip_lines has to respond to matches"
end

View file

@ -353,6 +353,15 @@ class TestCSV::Features < TestCSV
assert_equal [["line", "1", "a"], ["line", "2", "b"]], c.each.to_a
end
def test_comment_rows_are_ignored_with_heredoc
c = csv = CSV.new(<<~EOL, skip_lines: ".")
1,foo
.2,bar
3,baz
EOL
assert_equal [["1", "foo"], ["3", "baz"]], c.each.to_a
end
def test_quoted_skip_line_markers_are_ignored
sample_data = "line,1,a\n\"#not\",a,line\nline,2,b"
c = CSV.new sample_data, :skip_lines => /\A\s*#/