CodeSampleX

Exemplo

csv 3.3.2: Handle converters: :numeric parsing leading-zero strings as octal integers and non-octal digits as floats in CSV

Amostra verificada para gem csv 3.3.2: Handle converters: :numeric parsing leading-zero strings as octal integers and non-octal digits as floats in CSV. O…

sha256:7c404f5a7f67250133f23da7b4871d620cdb4ed856e88bdc48bfe81bf56f54fa

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
2
Chaves de assinatura que o compilaram
1
Ambiente declarado ruby linux x64 ruby ruby rubygems

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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

Caso

HOW
Objetivo
Handle converters: :numeric parsing leading-zero strings as octal integers and non-octal digits as floats in CSV
Pacotes
Símbolos
  • CSV.parse_line
  • CSV.parse
  • CSV::Converters
Ambiente
ruby
Criado
2026-08-16T15:32:30Z

Contrato

  1. CSV.parse_line with converters: :numeric parses leading-zero strings like 012 as octal integer 10, whereas strings with non-octal digits like 008 fall through to float 8.0 rather than parsing as decimal integers or strings
  2. converters: :integer leaves invalid-octal strings like 008 as unconverted strings whereas valid-octal strings like 012 are converted to integer 10
  3. converters: :numeric converts hexadecimal strings with 0x prefix to base-16 integers and binary strings with 0b prefix to base-2 integers
  4. converters: :numeric parses Ruby integer literal syntax with underscores like 1_000 into integer 1000
  5. converters option applies strictly to row field values leaving table header strings untouched
  6. unconverted_fields: true preserves pre-conversion raw string representations accessible via row.unconverted_fields

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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 covering Ruby standard library gem extraction (`sha256:8f9a1d4bba7bafae896c8862e6cc34aa3d64cb931492106f55207f395dde64bb`), table column indexing and hash emission (`sha256:466c2fd16d517415d38d889cd50560baa51c047e07cf6c380d184017e49bc546`), and whitespace/quote roundtripping (`sha256:25a66e52b7b289174635a51806d16ef38f17c9b1bb37d63fa67c4b218e537022`). None of the entries proved the behavior of `:numeric` or `:integer` converters on leading-zero and non-decimal numeric strings.

### What a model would have written instead
A model assumes `converters: :numeric` and `converters: :integer` parse all numeric strings like `"012"` and `"008"` using standard base-10 decimal integer conversions (or leave zero-padded strings untouched as Strings).

### How the wrong version fails
Silently converts zero-padded strings like `"012"` to octal integers (`10`), silently splits identical-column padded values across types (`"007"` becomes `Integer(7)` while `"008"` falls back to `Float(8.0)`), and silently parses hex/binary strings into integers, corrupting data pipelines while tests remain green.
csx.json
{"case":{"believed":"The CSV :numeric and :integer converters parse numeric string values using base-10 decimal representation.","caseId":"case:sha256:4d813a2210c09cb4f9bdbf35c5a11218485515f6e3368e8fe54376cd715c54d8","contract":["CSV.parse_line with converters: :numeric parses leading-zero strings like 012 as octal integer 10, whereas strings with non-octal digits like 008 fall through to float 8.0 rather than parsing as decimal integers or strings","converters: :integer leaves invalid-octal strings like 008 as unconverted strings whereas valid-octal strings like 012 are converted to integer 10","converters: :numeric converts hexadecimal strings with 0x prefix to base-16 integers and binary strings with 0b prefix to base-2 integers","converters: :numeric parses Ruby integer literal syntax with underscores like 1_000 into integer 1000","converters option applies strictly to row field values leaving table header strings untouched","unconverted_fields: true preserves pre-conversion raw string representations accessible via row.unconverted_fields"],"goal":"Handle converters: :numeric parsing leading-zero strings as octal integers and non-octal digits as floats in CSV","kind":"HOW","packages":["pkg:gem/csv@3.3.2"],"schemaVersion":1,"symbols":["CSV.parse_line","CSV.parse","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.parse","CSV::Converters"],"verifierAdapter":"rubygems@1"}
test/contract.rb
require "minitest/autorun"
require "csv"

class CsvNumericConverterOctalTrapTest < Minitest::Test
  def test_numeric_converter_parses_leading_zero_as_octal_and_falls_back_to_float
    # A naive model expects "012" -> 12 (or "012") and "008" -> 8 (or "008").
    # In Ruby CSV, :integer calls Kernel#Integer(s) without base 10, which parses
    # leading zeros as octal. "012" becomes 10 (1*8 + 2).
    # For "008", '8' is an invalid octal digit, so Integer("008") raises ArgumentError.
    # :numeric catches ArgumentError and falls through to Float("008"), which succeeds and yields 8.0.
    parsed = CSV.parse_line("012,008,077", converters: :numeric)

    assert_equal 10, parsed[0]
    refute_equal 12, parsed[0]
    assert_instance_of Integer, parsed[0]

    assert_equal 8.0, parsed[1]
    refute_instance_of Integer, parsed[1]
    assert_instance_of Float, parsed[1]

    assert_equal 63, parsed[2]
    assert_instance_of Integer, parsed[2]
  end

  def test_integer_converter_leaves_invalid_octal_as_string
    # Under converters: :integer alone, "012" converts to octal 10,
    # but "008" (invalid octal) fails Integer() and remains the unconverted String "008".
    parsed = CSV.parse_line("012,008", converters: :integer)

    assert_equal 10, parsed[0]
    assert_instance_of Integer, parsed[0]

    assert_equal "008", parsed[1]
    assert_instance_of String, parsed[1]
  end

  def test_numeric_converter_parses_hex_and_binary_prefixes
    # Kernel#Integer parses standard Ruby prefixes: 0x for hex, 0b for binary.
    parsed = CSV.parse_line("0x10,0b101", converters: :numeric)

    assert_equal 16, parsed[0]
    assert_instance_of Integer, parsed[0]

    assert_equal 5, parsed[1]
    assert_instance_of Integer, parsed[1]
  end

  def test_numeric_converter_supports_ruby_numeric_underscores
    # Kernel#Integer allows embedded underscores as digit separators.
    parsed = CSV.parse_line("1_000,50_000", converters: :numeric)

    assert_equal 1000, parsed[0]
    assert_equal 50000, parsed[1]
  end

  def test_converters_leave_header_names_untouched
    # converters: :numeric converts only field values, not headers.
    table = CSV.parse("012,008\n100,200\n", headers: true, converters: :numeric)

    # Headers remain raw Strings
    assert_equal ["012", "008"], table.headers
    assert_instance_of String, table.headers[0]

    # Row values are converted
    assert_equal 100, table[0]["012"]
    assert_equal 200, table[0]["008"]
  end

  def test_unconverted_fields_preserves_raw_string_values
    # unconverted_fields: true attaches the raw strings before conversion
    row = CSV.parse_line("012,008,0x10", converters: :numeric, unconverted_fields: true)

    assert_equal [10, 8.0, 16], row
    assert_equal ["012", "008", "0x10"], row.unconverted_fields
  end
end

Seeder de origem

csx-seed