Skip to content

Commit

Permalink
csv.rb: optimize CSV::Table#to_a and #to_csv
Browse files Browse the repository at this point in the history
* lib/csv.rb (CSV::Table#to_a, #to_csv): use Array#push instead of
  Array#concat for performance improvement. This performance improvement is
  proposed by zdennis <[email protected]>. The patch is from
  Mau Magnaguagno <[email protected]>.
  close ruby#946

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59657 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
glass committed Aug 25, 2017
1 parent c0baa38 commit 20c5f60
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -879,13 +879,11 @@ def ==(other)
# then all of the field rows will follow.
#
def to_a
@table.inject([headers]) do |array, row|
if row.header_row?
array
else
array + [row.fields]
end
array = [headers]
@table.each do |row|
array.push(row.fields) unless row.header_row?
end
return array
end

#
Expand All @@ -896,13 +894,11 @@ def to_a
# pass <tt>:write_headers => false</tt>.
#
def to_csv(write_headers: true, **options)
@table.inject(write_headers ? [headers.to_csv(options)] : [ ]) do |rows, row|
if row.header_row?
rows
else
rows + [row.fields.to_csv(options)]
end
end.join('')
array = write_headers ? [headers.to_csv(options)] : []
@table.each do |row|
array.push(row.fields.to_csv(options)) unless row.header_row?
end
return array.join('')
end
alias_method :to_s, :to_csv

Expand Down

0 comments on commit 20c5f60

Please sign in to comment.