0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2019-09-27

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

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?