-
Notifications
You must be signed in to change notification settings - Fork 115
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
Feature Request: Generate CSV String from Array #255
Comments
How about this? diff --git a/lib/csv/core_ext/array.rb b/lib/csv/core_ext/array.rb
index 8beb06b..0180deb 100644
--- a/lib/csv/core_ext/array.rb
+++ b/lib/csv/core_ext/array.rb
@@ -4,6 +4,16 @@ class Array # :nodoc:
# ["CSV", "data"].to_csv
# #=> "CSV,data\n"
def to_csv(**options)
- CSV.generate_line(self, **options)
+ case first
+ when Array
+ output = +""
+ csv = CSV.new(output, **options)
+ each do |row|
+ csv << row
+ end
+ output
+ else
+ CSV.generate_line(self, **options)
+ end
end
end If we use the suggested approach, I think that |
@kou I like |
OK. Do you want to work on this? Or does @ericgpks want to work on this? |
I would like to try this one !! |
OK! I'm waiting for your pull request for this! |
(ruby/csv#256) fix ruby/csv#255 Co-authored-by: Sutou Kouhei <[email protected]>
Problem
When we have
one simple way to generate CSV String from this Array is:
This works, but it's procedural and not intuitive.
It's often the case that it's easy to prepare data in form of Array, and difficult to prepare data in form of String, so it's good to have a way to directly convert Array into CSV String.
Solution
My suggestion is to add the method below:
Advantages
generate
to customize its behaviorDisadvantages
Implementtion
This implementation works in my local environment. It's not optimized anyway.
The text was updated successfully, but these errors were encountered: