summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-16 09:32:32 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-16 09:32:32 +0000
commit87acdae43dcfab5d28c85b10374be9faf6209076 (patch)
tree6e731ff9872d73900a67a9ac55aa04948dcf0ea2
parent3b77cb2a64315e5bb7b2eb9af08cbca5db434ce2 (diff)
Strip punctuation from CSV headers in symbol converter.
Patch by @cllns. [Fix GH-957] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58744 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--lib/csv.rb4
-rwxr-xr-xtest/csv/test_headers.rb7
2 files changed, 9 insertions, 2 deletions
diff --git a/lib/csv.rb b/lib/csv.rb
index 30e4b235869..336b42a6974 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -1014,8 +1014,8 @@ class CSV
HeaderConverters = {
downcase: lambda { |h| h.encode(ConverterEncoding).downcase },
symbol: lambda { |h|
- h.encode(ConverterEncoding).downcase.strip.gsub(/\s+/, "_").
- gsub(/\W+/, "").to_sym
+ h.encode(ConverterEncoding).downcase.gsub(/[^[\s\w]]+/, "").strip.
+ gsub(/\s+/, "_").to_sym
}
}
diff --git a/test/csv/test_headers.rb b/test/csv/test_headers.rb
index d8a1c15836f..5ec8932d954 100755
--- a/test/csv/test_headers.rb
+++ b/test/csv/test_headers.rb
@@ -225,6 +225,13 @@ class TestCSV::Headers < TestCSV
assert_equal([:one, :two_three], csv.headers)
end
+ def test_builtin_symbol_converter_with_punctuation
+ csv = CSV.parse( "One, Two & Three ($)", headers: true,
+ return_headers: true,
+ header_converters: :symbol )
+ assert_equal([:one, :two_three], csv.headers)
+ end
+
def test_builtin_converters_with_blank_header
csv = CSV.parse( "one,,three", headers: true,
return_headers: true,