From: kou@cozmixng•org
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:50486] [Ruby trunk Bug#14482][Closed] headerが重複したCSVをheaders:trueでparseした場合、row.to_h[key] と row[key] の結果が一致しない
Date: Sun, 25 Feb 2018 04:28:54 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-70654.20180225042852.1836b4ba6d5c331a@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14482.20180216115010@ruby-lang.org
Issue #14482 has been updated by kou (Kouhei Sutou).
Status changed from Open to Closed
Assignee set to kou (Kouhei Sutou)
後方互換性がなくなってしまうのでどうするかなぁと考えてみたんですが、もともと重複していた場合はなにを返すかわからない、という仕様だったので変わっても大丈夫かなぁと言う気持ちになったので、同じになるようにしました。
----------------------------------------
Bug #14482: headerが重複したCSVをheaders:trueでparseした場合、row.to_h[key] と row[key] の結果が一致しない
https://bugs.ruby-lang.org/issues/14482#change-70654
* Author: tompng (tomoya ishida)
* Status: Closed
* Priority: Normal
* Assignee: kou (Kouhei Sutou)
* Target version:
* ruby -v: ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin16]
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
1行目に重複したkeyがあるcsvファイルをheaders: trueでparseした場合、
row[key]とrow.to_h[key]が異なる値を返します。
~~~ruby
require 'csv'
invalid_header_csv = "id,name,name\n1,ichiro,ICHIRO\n2,jack,JACK"
rows = CSV.parse invalid_header_csv, headers: true
puts rows[0]['name'] #=> ichiro
puts rows[0].to_h['name'] #=> ICHIRO
~~~
ドキュメントには
~~~
to_hash -> Hash
自身をシンプルなハッシュに変換します。
フィールドの順序は無視されます。 重複したフィールドは削除されます。
~~~
とありますが、削除されるフィールドとrow[key]で無視されるフィールドは一致している方が良いのではないかと思います。
~~~ruby
class CSV::Row
def to_h
headers.uniq.map { |key| [key, self[key]] }.to_h
end
alias_method :to_hash, :to_h
end
~~~
--
https://bugs.ruby-lang.org/
prev parent reply other threads:[~2018-02-25 4:29 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-14482.20180216115010@ruby-lang.org>
2018-02-16 11:50 ` [ruby-dev:50475] [Ruby trunk Bug#14482] headerが重複したCSVをheaders:trueでparseした場合、row.to_h[key] と row[key] の結果が一致しない tomoyapenguin
2018-02-25 4:28 ` kou [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-70654.20180225042852.1836b4ba6d5c331a@ruby-lang.org \
--to=ruby-dev@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).