Skip to content

Document pattern matching on parsed CSV rows - #383

Open
Sinkleberg wants to merge 2 commits into
ruby:mainfrom
Sinkleberg:codex/csv-row-pattern-recipe
Open

Sinkleberg wants to merge 2 commits into
ruby:mainfrom
Sinkleberg:codex/csv-row-pattern-recipe

Conversation

@Sinkleberg

Copy link
Copy Markdown

Adds a filtering recipe for matching parsed CSV rows by header or position. The header example uses header_converters: :symbol, as suggested in #246.

The examples and both test suites passed on Ruby 3.3.13. I also checked the recipe in the generated docs.

@kou

kou commented Nov 8, 2026

Copy link
Copy Markdown
Member

Could you update this?

  • doc/csv/recipes/filtering.rdoc isn't a good place for this. "Filtering" in it means a Unix-style filter that reads CSV and writes CSV. Could you move this to doc/csv/recipes/parsing.rdoc and use the "Recipe: ..." heading style like other recipes?

  • header_converters: :symbol doesn't just convert headers to symbols. It also downcases them and replaces spaces with _. For example, "First Name" is converted to :first_name. So Name: doesn't match even with header_converters: :symbol. Could you describe it? We can also mention that header_converters: ->(header) {header.to_sym} keeps the original case. The comment in lib/csv/row.rb has the same problem.

  • Could you remove the tests? test_patterns_with_string_headers asserts that a symbol key doesn't match a string header but CSV::Row pattern matching Symbol assumption #246 is still open to discuss changing it. We don't want to fix the current behavior by a test. Other cases are already covered by existing tests. So this can be a document only change.

  • "This example uses Ruby 3.0 or later, where +in+ returns +true+ or +false+." can be just "This example requires Ruby 3.0 or later."

  • "Header conversion is opt-in: CSV::Row preserves the header key types supplied by the parser." is redundant. "Headers are strings by default. So a symbol key doesn't match:" is enough.

@Sinkleberg

Copy link
Copy Markdown
Author

I’ll move the recipe to the parsing docs and drop the new tests. I’ll also fix both examples to show how the built-in converter turns First Name into :first_name.

@Sinkleberg

Copy link
Copy Markdown
Author

Moved the recipe to the parsing docs and removed the added tests. The examples now show both how First Name becomes :first_name and how to keep the original case. The examples and the CSV test suite pass.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants