Help us understand the problem. What is going on with this article?

#Ruby 2.5 では 1行のBodyだけのCSV ファイルで headers が取得できないっぽい? ( Ruby 2.6 では可能 )

Unable to headers with only one line no body CSV file in Ruby 2.5

Ruby 2.5

require 'csv'

CSV.parse("A,B,C", headers: true).headers
# => []

CSV.parse("A,B,C\nX,Y,Z", headers: true).headers
# => ["A", "B", "C"]

CSV.parse("A,B,C", headers: true).to_s
# => "\n"

以下で動作確認。

  • ruby 2.5.2p104 (2018-10-18 revision 65133) [x86_64-darwin18]
  • ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]
  • ruby 2.5.6p201 (2019-08-28 revision 67796) [x86_64-linux] (docker+mac)

Ruby 2.6

require 'csv'

CSV.parse("A,B,C", headers: true).headers
# => ["A", "B", "C"]

CSV.parse("A,B,C\nX,Y,Z", headers: true).headers
# => ["A", "B", "C"]

CSV.parse("A,B,C", headers: true).to_s
# => "A,B,C\n"

以下で動作確認。

  • ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin18]
  • ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]
  • ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
  • ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux] (docker+Mac)

Docs

どこに違いがあるのだろう?

https://ruby-doc.org/stdlib-2.5.3/libdoc/csv/rdoc/CSV.html#method-i-headers
https://ruby-doc.org/stdlib-2.6.1/libdoc/csv/rdoc/CSV.html#method-i-headers

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2534

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした