Skip to content

Preserve integer and range header values in Row#to_h - #387

Open
FFspiky wants to merge 1 commit into
ruby:mainfrom
FFspiky:fix/row-hash-header-lookup
Open

FFspiky wants to merge 1 commit into
ruby:mainfrom
FFspiky:fix/row-hash-header-lookup

Conversation

@FFspiky

@FFspiky FFspiky commented Nov 16, 2026

Copy link
Copy Markdown

CSV::Row#to_h retrieves each value with self[key], which treats integer and range headers as column selectors. Numeric headers produced by a custom header converter therefore lose or misassign their values:

row = CSV.parse_line("2024,2025\n5,6\n",
                     headers: true,
                     header_converters: ->(header) { Integer(header) },
                     converters: :integer)
row.to_h # currently {2024 => nil, 2025 => nil}; expected {2024 => 5, 2025 => 6}

Use header-only fetch(key) in both hash-conversion paths. This preserves the first matching value for repeated headers and the existing block-call/key-transformation behavior discussed in #356. Regression tests cover integer and range headers with/without a block, the parsing example, and repeated integer headers with transformed output keys.

Validation on macOS arm64:

  • The six regression tests fail against unchanged main (afbee5e3548d38914706ae0de8a6cf421ba43187).
  • Ruby 3.4.9: ruby run-test.rb and CSV_PARSER_SCANNER_TEST=yes ruby run-test.rb each pass 541 tests / 4,073 assertions.
  • Ruby 2.6.10: the row suite passes 39 tests / 185 assertions.
  • Gem build and git diff --check pass. rake warning:error rdoc exits successfully; it reports the same two unresolved ARGF/dig_methods.rdoc references as unchanged main.

Other Ruby versions and operating systems were not tested locally.

AI use: OpenAI Codex prepared the patch, tests, and description, reviewed the existing duplicate-header discussion, and ran these checks.

@FFspiky

FFspiky commented Nov 16, 2026

Copy link
Copy Markdown
Author

The test workflows are waiting for maintainer approval. Could you approve the run when convenient? Local validation on this head passes 541 tests in each parser mode on Ruby 3.4.9; the Ruby 2.6.10 row suite also passes. The broader OS/Ruby matrix has not run yet.

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.

1 participant