Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vitaliiorlov opened this issue Dec 13, 2022 · 1 comment

Comments

@vitaliiorlov
Copy link

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"
@zvory
Copy link
Owner

zvory commented May 18, 2024

if anyone wants to put up a PR to fix this with some tests i will merge it

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

No branches or pull requests

2 participants