ruby-changes:31744
From: jeg2 <ko1@a...>
Date: Sun, 24 Nov 2013 08:12:18 +0900 (JST)
Subject: [ruby-changes:31744] jeg2:r43823 (trunk): * lib/csv.rb: If skip_lines is set to a String, convert it to a Regexp
jeg2 2013-11-24 08:12:11 +0900 (Sun, 24 Nov 2013) New Revision: 43823 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43823 Log: * lib/csv.rb: If skip_lines is set to a String, convert it to a Regexp to prevent the alternative, which is that each line in the CSV gets converted to a Regexp when calling skip_lines#match. Modified files: trunk/ChangeLog trunk/lib/csv.rb trunk/test/csv/test_features.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 43822) +++ ChangeLog (revision 43823) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Nov 23 13:38:00 2013 Kyle Stevens <kstevens715@g...> + + * lib/csv.rb: If skip_lines is set to a String, convert it to a Regexp + to prevent the alternative, which is that each line in the CSV gets + converted to a Regexp when calling skip_lines#match. + Sun Nov 24 01:03:00 2013 Kenta Murata <mrkn@m...> * ext/bigdecimal/bigdecimal.c (BigDecimal_power): Use FIX2LONG instead Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 43822) +++ lib/csv.rb (revision 43823) @@ -1470,11 +1470,12 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1470 # <b><tt>:skip_lines</tt></b>:: When set to an object responding to # <tt>match</tt>, every line matching # it is considered a comment and ignored - # during parsing. When set to +nil+ - # no line is considered a comment. - # If the passed object does not respond - # to <tt>match</tt>, <tt>ArgumentError</tt> - # is thrown. + # during parsing. When set to a String, + # it is first converted to a Regexp. + # When set to +nil+ no line is considered + # a comment. If the passed object does + # not respond to <tt>match</tt>, + # <tt>ArgumentError</tt> is thrown. # # See CSV::DEFAULT_OPTIONS for the default settings. # @@ -2120,10 +2121,12 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L2121 # Stores the pattern of comments to skip from the provided options. # # The pattern must respond to +.match+, else ArgumentError is raised. + # Strings are converted to a Regexp. # # See also CSV.new def init_comments(options) @skip_lines = options.delete(:skip_lines) + @skip_lines = Regexp.new(@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 Index: test/csv/test_features.rb =================================================================== --- test/csv/test_features.rb (revision 43822) +++ test/csv/test_features.rb (revision 43823) @@ -299,12 +299,19 @@ class TestCSV::Features < TestCSV https://github.com/ruby/ruby/blob/trunk/test/csv/test_features.rb#L299 def test_comment_rows_are_ignored sample_data = "line,1,a\n#not,a,line\nline,2,b\n #also,no,line" c = CSV.new sample_data, :skip_lines => /\A\s*#/ - assert_equal c.each.to_a, [["line", "1", "a"], ["line", "2", "b"]] + assert_equal [["line", "1", "a"], ["line", "2", "b"]], 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*#/ - assert_equal c.each.to_a, [["line", "1", "a"], ["#not", "a", "line"], ["line", "2", "b"]] + assert_equal [["line", "1", "a"], ["#not", "a", "line"], ["line", "2", "b"]], c.each.to_a end + + def test_string_works_like_a_regexp + sample_data = "line,1,a\n#(not,a,line\nline,2,b\n also,#no,line" + c = CSV.new sample_data, :skip_lines => "#" + assert_equal [["line", "1", "a"], ["line", "2", "b"]], c.each.to_a + end + end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/