-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Include Enumerable in CSV #3465
Conversation
Let me know if I should add comments about justification to the commit message. |
You can write it here. You can also report an issue first, and then reference it in both the commit message and the PR, which usually makes it easier to track things. |
Include Enumerable in CSV in order to be able to use `each_with_index`, `map_with_index`, etc. Includes a test for `map_with_index`.
f43f397
to
eb38aa7
Compare
Updated commit message to explain justification and made test more robust. |
@tristil Thank you! But CSV is not an enumerable. An instance of CSV is more like a cursor that always points to a row and you can fetch any of the columns by index, header name or regex. That's why For an Enumerable (well, Iterator), you can use CSV.each_row(string_or_io).with_index.each { |row, index| ... } |
@asterite Okay, I understand now, and I see that this is well documented on the class. What confused me, besides not reading the documentation well, is that there is an While I'm here, would it make sense to implement the |
@tristil The headers functionality is already there. What do you need to do? |
@asterite So my new code is require "csv"
def get_rows
text = File.read("vendors.csv")
CSV.each_row(text).map_with_index do |row, index|
next if index == 0
row
end.compact
end
rows = get_rows The only reason I need the index is to skip the first row, which I would get for free with |
@asterite I see now that wouldn't work unless it was returning hashes instead of |
Just for note, you can also use CSV.each_row(text).skip(1).to_a And I think |
@chaniks That |
Include Enumerable in CSV, and add a test for
map_with_index
.