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

require 'csv'; CSV.generate { |csv| csv << {a: 1, b: 2} } throws undefined method 'collect' for nil #314

Open
dorianmariecom opened this issue Aug 24, 2024 · 1 comment

Comments

@dorianmariecom
Copy link

What's weird is that it works with structs (and arrays of course) but not with hashes

>> require 'csv'; CSV.generate { |csv| csv << {a: 1, b: 2} }
/Users/dorianmariefr/.asdf/installs/ruby/3.3.4/lib/ruby/gems/3.3.0/gems/csv-3.3.0/lib/csv/writer.rb:36:in `<<': undefined method `collect' for nil (NoMethodError)

        row = @headers.collect {|header| row[header]}
                      ^^^^^^^^
	from /Users/dorianmariefr/.asdf/installs/ruby/3.3.4/lib/ruby/gems/3.3.0/gems/csv-3.3.0/lib/csv.rb:2373:in `<<'
	from (irb):2:in `block in <top (required)>'
	from /Users/dorianmariefr/.asdf/installs/ruby/3.3.4/lib/ruby/gems/3.3.0/gems/csv-3.3.0/lib/csv.rb:1410:in `generate'
	from (irb):2:in `<main>'
	from <internal:kernel>:187:in `loop'
	from /Users/dorianmariefr/.asdf/installs/ruby/3.3.4/lib/ruby/gems/3.3.0/gems/irb-1.14.0/exe/irb:9:in `<top (required)>'
	from /Users/dorianmariefr/.asdf/installs/ruby/3.3.4/bin/irb:25:in `load'
	from /Users/dorianmariefr/.asdf/installs/ruby/3.3.4/bin/irb:25:in `<main>'
@kou
Copy link
Member

kou commented Aug 26, 2024

You need to specify headers:: CSV.generate(headers: [:a, :b]) {|csv| csv << {a: 1, b: 2}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants