diff options
author | Burdette Lamar <BurdetteLamar@Yahoo.com> | 2020-09-11 16:36:01 -0500 |
---|---|---|
committer | Sutou Kouhei <kou@cozmixng.org> | 2020-11-24 09:33:55 +0900 |
commit | 614afb1647d9c9eb170262c8b033f000c5beb6f0 (patch) | |
tree | d8edc7ead9f0bb31ba8ccb806b83e6feef9216ac /lib/csv.rb | |
parent | 207f2acc1355dea1fc1f483e4d8ff3e571a0ad89 (diff) |
[ruby/csv] Fix CSV.filter to preserve headers (#174)
Co-authored-by: Sutou Kouhei <kou@clear-code.com>
https://github.com/ruby/csv/commit/203c5e0574
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3804
Diffstat (limited to 'lib/csv.rb')
-rw-r--r-- | lib/csv.rb | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/csv.rb b/lib/csv.rb index 143909a099..cfd1b8621e 100644 --- a/lib/csv.rb +++ b/lib/csv.rb @@ -1057,10 +1057,29 @@ class CSV out_options[key] = value end end + # build input and output wrappers - input = new(input || ARGF, **in_options) + input = new(input || ARGF, **in_options) output = new(output || $stdout, **out_options) + # process headers + need_manual_header_output = + (in_options[:headers] and + out_options[:headers] == true and + out_options[:write_headers]) + if need_manual_header_output + first_row = input.shift + if first_row + if first_row.is_a?(Row) + headers = first_row.headers + yield headers + output << headers + end + yield first_row + output << first_row + end + end + # read, yield, write input.each do |row| yield row |