csv-freeze-headers.patch

Andrew Vit, 11/23/2013 05:05 AM

Download (1.33 KB)

View differences:

lib/csv.rb
2205 2205
      # prepare converted and unconverted copies
2206 2206
      row      = @headers                       if row.nil?
2207 2207
      @headers = convert_fields(@headers, true)
2208
      @headers.map { |h| h.freeze if h.is_a? String }
2208 2209

  
2209 2210
      if @return_headers                                     # return headers
2210 2211
        return self.class::Row.new(@headers, row, true)
test/csv/test_interface.rb
198 198
    end
199 199
  end
200 200

  
201
  def test_write_hash_with_string_keys
202
    File.unlink(@path)
203

  
204
    lines = [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}]
205
    CSV.open( @path, "wb", headers: true ) do |csv|
206
      csv << lines.first.keys
207
      lines.each { |line| csv << line }
208
    end
209
    CSV.open( @path, "rb", headers: true ) do |csv|
210
      csv.each do |line|
211
        csv.headers.each_with_index do |header, h|
212
          keys = line.to_hash.keys
213
          assert_equal(String, keys[h].class)
214
          assert_equal(header.object_id, keys[h].object_id)
215
        end
216
      end
217
    end
218
  end
219

  
201 220
  def test_write_hash_with_headers_array
202 221
    File.unlink(@path)
203 222