diff --git a/lib/csv.rb b/lib/csv.rb index de056d46f6..25f2bf35e6 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -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 diff --git a/test/csv/test_features.rb b/test/csv/test_features.rb index a1f1ddbd15..3f9359faaf 100755 --- a/test/csv/test_features.rb +++ b/test/csv/test_features.rb @@ -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*#/