CodeSampleX

Sample

csv 3.3.2: Handle numeric octal versus float bifurcation, single nil row truncation, and date-time type coercion across CSV serialization and parsing round trips

Verified sample for gem csv 3.3.2: Handle numeric octal versus float bifurcation, single nil row truncation, and date-time type coercion across CSV…

sha256:fd8cc5b68025f317dc1fd1a73f3b93f524eabbdbe8042318706daa5eccc80012

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
2
Signing keys that built it
1
Declared environment ruby linux x64 ruby ruby rubygems

Verification-run environments

Environment Contract Stages Run
ruby 3 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · rubygems@1
2026-08-16
ruby 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 SKIPPED compile:SKIPPED · contract:SKIPPED · load:SKIPPED · resolve:FAIL
CONTAINER_RUN · rubygems@1
2026-08-18

Case

HOW
Goal
Handle numeric octal versus float bifurcation, single nil row truncation, and date-time type coercion across CSV serialization and parsing round trips
Packages
Symbols
  • CSV.parse_line
  • CSV.generate_line
  • CSV::Converters
Environment
ruby
Created
2026-08-16T15:31:05Z

Contract

  1. CSV.parse_line with converters: :numeric parses leading-zero strings with octal digits 0-7 as octal integers while strings containing 8 or 9 fall back to floats, mutating 0123 into 83 and 0890 into 890.0 across a round trip
  2. assert CSV.parse_line with converters: :numeric coerces hex and binary string literals into decimal integers
  3. assert CSV.generate_line with a single nil element emits an empty line that CSV.parse_line reads as an empty array of length zero
  4. assert CSV.parse_line with converters: :all converts Date strings into DateTime instances and leaves Time strings as unparsed String objects
  5. assert CSV.parse_line with unconverted_fields: true retains the original string representations on the unconverted_fields attribute

Files

  • Gemfile
  • NOTES.md
  • csx.json
  • test/contract.rb

Download the source artifact (tar.gz)

Source

Gemfile
source "https://rubygems.org"

gem "csv", "3.3.2"
gem "minitest", "5.27.0"
NOTES.md
# Notes

### What `search_known_solution` answered
`search_known_solution` returned existing samples `sha256:8f9a1d4bba7bafae896c8862e6cc34aa3d64cb931492106f55207f395dde64bb` (resolving `LoadError` following standard library gem extraction), `sha256:1576a46e74dec37c847b8fc94f319abfbfaf7bd12609c46b9703ce3846ad1295` (header return mode in table column indexing), and `sha256:25a66e52b7b289174635a51806d16ef38f17c9b1bb37d63fa67c4b218e537022` (whitespace stripping and row hash collision rules).

### What a model would have written instead
A model assumes `converters: :numeric` parses zero-padded decimal strings (such as `"0123"`) as decimal integers or preserves them as strings, that a single-column nil row `[nil]` round-trips as a 1-element array, and that `converters: :all` preserves `Date` objects as `Date` instances.

### How the wrong version fails
Fails silently with corrupted data on green builds: `"0123"` is parsed as octal integer `83` while `"0890"` becomes float `890.0`, single-element `[nil]` rows are silently truncated to `[]` (length 0), and `Date` instances are coerced into `DateTime` objects.
csx.json
{"case":{"believed":"Passing converters: :numeric parses numeric strings with leading zeros as decimal numbers or preserves them consistently as strings.","caseId":"case:sha256:9303a81594358ff9955e456fb2f701b0d0b772564d35a1b28c6262489e5d8c2d","contract":["CSV.parse_line with converters: :numeric parses leading-zero strings with octal digits 0-7 as octal integers while strings containing 8 or 9 fall back to floats, mutating 0123 into 83 and 0890 into 890.0 across a round trip","assert CSV.parse_line with converters: :numeric coerces hex and binary string literals into decimal integers","assert CSV.generate_line with a single nil element emits an empty line that CSV.parse_line reads as an empty array of length zero","assert CSV.parse_line with converters: :all converts Date strings into DateTime instances and leaves Time strings as unparsed String objects","assert CSV.parse_line with unconverted_fields: true retains the original string representations on the unconverted_fields attribute"],"goal":"Handle numeric octal versus float bifurcation, single nil row truncation, and date-time type coercion across CSV serialization and parsing round trips","kind":"HOW","packages":["pkg:gem/csv@3.3.2"],"schemaVersion":1,"symbols":["CSV.parse_line","CSV.generate_line","CSV::Converters"]},"contractCommand":["ruby","test/contract.rb"],"environment":{"arch":"x64","ecosystem":"gem","executionContext":"ruby","language":"ruby","os":"linux","packageManager":"rubygems","runtime":"ruby","schemaVersion":1},"license":"MIT-0","packages":["pkg:gem/csv@3.3.2"],"schemaVersion":1,"symbols":["CSV.parse_line","CSV.generate_line","CSV::Converters"],"verifierAdapter":"rubygems@1"}
test/contract.rb
require "minitest/autorun"
require "csv"
require "date"
require "time"

class CsvSerializationBoundaryTest < Minitest::Test
  # 1. Numeric octal vs float bifurcation trap on leading zero strings
  # A naive model expects converters: :numeric to parse leading-zero strings as decimal
  # integers (e.g., "0123" -> 123) or leave them as strings.
  # Instead, Integer(f) in Ruby treats leading zeros as octal literals. Strings containing
  # only octal digits (0-7) become octal Integers ("0123" -> 83, "0755" -> 493).
  # Strings containing invalid octal digits 8 or 9 fail Integer(f) and fall through to
  # Float(f), becoming Floats ("0890" -> 890.0, "0758" -> 758.0).
  # Upon re-serialization with CSV.generate_line, "0123" becomes "83" and "0890" becomes "890.0".
  def test_numeric_converter_octal_vs_float_bifurcation_on_leading_zeros
    # Octal digits (0-7) parse as octal Integers
    parsed_octal = CSV.parse_line("0123", converters: :numeric)
    assert_equal [83], parsed_octal
    assert_instance_of Integer, parsed_octal.first
    refute_equal [123], parsed_octal

    roundtrip_octal = CSV.generate_line(parsed_octal).chomp
    assert_equal "83", roundtrip_octal

    # Digits 8-9 fail octal Integer parsing and cascade to Float
    parsed_float = CSV.parse_line("0890", converters: :numeric)
    assert_equal [890.0], parsed_float
    assert_instance_of Float, parsed_float.first

    roundtrip_float = CSV.generate_line(parsed_float).chomp
    assert_equal "890.0", roundtrip_float

    # Further bifurcation examples
    assert_equal [493], CSV.parse_line("0755", converters: :numeric)
    assert_equal [758.0], CSV.parse_line("0758", converters: :numeric)
  end

  # 2. Hexadecimal, binary, and underscore string coercion
  # Integer(f) coerces hex ("0x1A" -> 26), binary ("0b101" -> 5), and underscore ("1_000" -> 1000).
  # On serialization, radix prefixes and underscores are permanently lost.
  def test_numeric_converter_coerces_radix_prefixes_and_underscores
    parsed = CSV.parse_line("0x1A,0b101,1_000", converters: :numeric)
    assert_equal [26, 5, 1000], parsed
    assert parsed.all? { |v| v.is_a?(Integer) }

    serialized = CSV.generate_line(parsed).chomp
    assert_equal "26,5,1000", serialized
  end

  # 3. Single nil element row silent column truncation
  # A single-element nil row [nil] serializes to an empty line "\n".
  # When parsed back with CSV.parse_line, it returns an empty Array [] with length 0,
  # silently dropping the single nil column.
  # By contrast, multi-element nil rows [nil, nil] serialize as ",\n" and round-trip to [nil, nil].
  # Quoted empty string [""] serializes as "\"\"\n" and round-trips to [""].
  def test_single_nil_row_truncation_asymmetry
    single_nil_row = [nil]
    serialized_single_nil = CSV.generate_line(single_nil_row)
    assert_equal "\n", serialized_single_nil

    parsed_single_nil = CSV.parse_line(serialized_single_nil)
    assert_equal [], parsed_single_nil
    assert_equal 0, parsed_single_nil.length
    refute_equal single_nil_row, parsed_single_nil

    # Multi-element nil rows retain column structure
    multi_nil_row = [nil, nil]
    serialized_multi_nil = CSV.generate_line(multi_nil_row)
    assert_equal ",\n", serialized_multi_nil
    assert_equal [nil, nil], CSV.parse_line(serialized_multi_nil)

    # Empty string retains single column structure
    empty_str_row = [""]
    serialized_empty_str = CSV.generate_line(empty_str_row)
    assert_equal "\"\"\n", serialized_empty_str
    assert_equal [""], CSV.parse_line(serialized_empty_str)
  end

  # 4. converters: :all type boundary (Date vs DateTime vs Time)
  # CSV::Converters[:all] is defined as [:date_time, :numeric].
  # Date instances serialize to "YYYY-MM-DD" and round-trip through :all as DateTime, not Date.
  # Time instances serialize via to_s ("YYYY-MM-DD HH:MM:SS UTC"), failing DateTimeMatcher,
  # and round-trip through :all as unparsed String objects.
  def test_all_converter_date_time_type_boundaries
    date_val = Date.new(2026, 8, 16)
    gen_date = CSV.generate_line([date_val])
    parsed_date = CSV.parse_line(gen_date, converters: :all).first

    assert_instance_of DateTime, parsed_date
    refute_instance_of Date, parsed_date unless parsed_date.class == Date # DateTime is a subclass of Date
    assert_equal "DateTime", parsed_date.class.name

    time_val = Time.utc(2026, 8, 16, 12, 0, 0)
    gen_time = CSV.generate_line([time_val])
    parsed_time = CSV.parse_line(gen_time, converters: :all).first

    assert_instance_of String, parsed_time
    assert_equal "2026-08-16 12:00:00 UTC", parsed_time
  end

  # 5. unconverted_fields preserves raw string representations
  # When converters are enabled, unconverted_fields: true attaches the raw unmodified
  # string values to the .unconverted_fields attribute, allowing callers to recover the
  # original format before octal/numeric coercion.
  def test_unconverted_fields_preserves_pristine_representations
    raw_csv = "0123,0890,0x1A,2026-08-16"
    parsed = CSV.parse_line(raw_csv, converters: :all, unconverted_fields: true)

    assert_equal [83, 890.0, 26, DateTime.parse("2026-08-16")], parsed
    assert_equal ["0123", "0890", "0x1A", "2026-08-16"], parsed.unconverted_fields
  end
end

Origin Seeder

csx-seed