Skip to content

Instance variable @headers is nil at csv-safe.rb:56:in `sanitize_row' #15

@vitaliiorlov

Description

@vitaliiorlov

Hello!
It seems I found a bug, @headers variable is nil at csv-safe.rb:56:in `sanitize_row'. Looks like the fix is to read headers as a method.

headers = %i[a b c]
[
    [0] :a,
    [1] :b,
    [2] :c
]

payload = { b: :b, a: :a, c: :c }
{
    :b => :b,
    :a => :a,
    :c => :c
}

CSVSafe.generate(headers: true) { |csv| csv << headers; csv << payload }
NoMethodError: undefined method `map' for nil:NilClass
from /Users/.../.rbenv/versions/2.7.6/lib/ruby/gems/2.7.0/gems/csv-safe-3.1.1/lib/csv-safe.rb:56:in `sanitize_row'

After I changed @headers to headers (then headers.map { |header| sanitize_field(row[header]) }) it works:

headers = %i[a b c]
[
    [0] :a,
    [1] :b,
    [2] :c
]

payload = { b: :b, a: :a, c: :c }
{
    :b => :b,
    :a => :a,
    :c => :c
}

CSVSafe.generate(headers: true) { |csv| csv << headers; csv << payload }
"a,b,c\na,b,c\n"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions