Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: csv.rb
# encoding: US-ASCII
[0] Fix | Delete
# = csv.rb -- CSV Reading and Writing
[1] Fix | Delete
#
[2] Fix | Delete
# Created by James Edward Gray II on 2005-10-31.
[3] Fix | Delete
# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
[4] Fix | Delete
# under the terms of Ruby's license.
[5] Fix | Delete
#
[6] Fix | Delete
# See CSV for documentation.
[7] Fix | Delete
#
[8] Fix | Delete
# == Description
[9] Fix | Delete
#
[10] Fix | Delete
# Welcome to the new and improved CSV.
[11] Fix | Delete
#
[12] Fix | Delete
# This version of the CSV library began its life as FasterCSV. FasterCSV was
[13] Fix | Delete
# intended as a replacement to Ruby's then standard CSV library. It was
[14] Fix | Delete
# designed to address concerns users of that library had and it had three
[15] Fix | Delete
# primary goals:
[16] Fix | Delete
#
[17] Fix | Delete
# 1. Be significantly faster than CSV while remaining a pure Ruby library.
[18] Fix | Delete
# 2. Use a smaller and easier to maintain code base. (FasterCSV eventually
[19] Fix | Delete
# grew larger, was also but considerably richer in features. The parsing
[20] Fix | Delete
# core remains quite small.)
[21] Fix | Delete
# 3. Improve on the CSV interface.
[22] Fix | Delete
#
[23] Fix | Delete
# Obviously, the last one is subjective. I did try to defer to the original
[24] Fix | Delete
# interface whenever I didn't have a compelling reason to change it though, so
[25] Fix | Delete
# hopefully this won't be too radically different.
[26] Fix | Delete
#
[27] Fix | Delete
# We must have met our goals because FasterCSV was renamed to CSV and replaced
[28] Fix | Delete
# the original library as of Ruby 1.9. If you are migrating code from 1.8 or
[29] Fix | Delete
# earlier, you may have to change your code to comply with the new interface.
[30] Fix | Delete
#
[31] Fix | Delete
# == What's Different From the Old CSV?
[32] Fix | Delete
#
[33] Fix | Delete
# I'm sure I'll miss something, but I'll try to mention most of the major
[34] Fix | Delete
# differences I am aware of, to help others quickly get up to speed:
[35] Fix | Delete
#
[36] Fix | Delete
# === CSV Parsing
[37] Fix | Delete
#
[38] Fix | Delete
# * This parser is m17n aware. See CSV for full details.
[39] Fix | Delete
# * This library has a stricter parser and will throw MalformedCSVErrors on
[40] Fix | Delete
# problematic data.
[41] Fix | Delete
# * This library has a less liberal idea of a line ending than CSV. What you
[42] Fix | Delete
# set as the <tt>:row_sep</tt> is law. It can auto-detect your line endings
[43] Fix | Delete
# though.
[44] Fix | Delete
# * The old library returned empty lines as <tt>[nil]</tt>. This library calls
[45] Fix | Delete
# them <tt>[]</tt>.
[46] Fix | Delete
# * This library has a much faster parser.
[47] Fix | Delete
#
[48] Fix | Delete
# === Interface
[49] Fix | Delete
#
[50] Fix | Delete
# * CSV now uses Hash-style parameters to set options.
[51] Fix | Delete
# * CSV no longer has generate_row() or parse_row().
[52] Fix | Delete
# * The old CSV's Reader and Writer classes have been dropped.
[53] Fix | Delete
# * CSV::open() is now more like Ruby's open().
[54] Fix | Delete
# * CSV objects now support most standard IO methods.
[55] Fix | Delete
# * CSV now has a new() method used to wrap objects like String and IO for
[56] Fix | Delete
# reading and writing.
[57] Fix | Delete
# * CSV::generate() is different from the old method.
[58] Fix | Delete
# * CSV no longer supports partial reads. It works line-by-line.
[59] Fix | Delete
# * CSV no longer allows the instance methods to override the separators for
[60] Fix | Delete
# performance reasons. They must be set in the constructor.
[61] Fix | Delete
#
[62] Fix | Delete
# If you use this library and find yourself missing any functionality I have
[63] Fix | Delete
# trimmed, please {let me know}[mailto:james@grayproductions.net].
[64] Fix | Delete
#
[65] Fix | Delete
# == Documentation
[66] Fix | Delete
#
[67] Fix | Delete
# See CSV for documentation.
[68] Fix | Delete
#
[69] Fix | Delete
# == What is CSV, really?
[70] Fix | Delete
#
[71] Fix | Delete
# CSV maintains a pretty strict definition of CSV taken directly from
[72] Fix | Delete
# {the RFC}[http://www.ietf.org/rfc/rfc4180.txt]. I relax the rules in only one
[73] Fix | Delete
# place and that is to make using this library easier. CSV will parse all valid
[74] Fix | Delete
# CSV.
[75] Fix | Delete
#
[76] Fix | Delete
# What you don't want to do is feed CSV invalid data. Because of the way the
[77] Fix | Delete
# CSV format works, it's common for a parser to need to read until the end of
[78] Fix | Delete
# the file to be sure a field is invalid. This eats a lot of time and memory.
[79] Fix | Delete
#
[80] Fix | Delete
# Luckily, when working with invalid CSV, Ruby's built-in methods will almost
[81] Fix | Delete
# always be superior in every way. For example, parsing non-quoted fields is as
[82] Fix | Delete
# easy as:
[83] Fix | Delete
#
[84] Fix | Delete
# data.split(",")
[85] Fix | Delete
#
[86] Fix | Delete
# == Questions and/or Comments
[87] Fix | Delete
#
[88] Fix | Delete
# Feel free to email {James Edward Gray II}[mailto:james@grayproductions.net]
[89] Fix | Delete
# with any questions.
[90] Fix | Delete
[91] Fix | Delete
require "forwardable"
[92] Fix | Delete
require "English"
[93] Fix | Delete
require "date"
[94] Fix | Delete
require "stringio"
[95] Fix | Delete
[96] Fix | Delete
#
[97] Fix | Delete
# This class provides a complete interface to CSV files and data. It offers
[98] Fix | Delete
# tools to enable you to read and write to and from Strings or IO objects, as
[99] Fix | Delete
# needed.
[100] Fix | Delete
#
[101] Fix | Delete
# == Reading
[102] Fix | Delete
#
[103] Fix | Delete
# === From a File
[104] Fix | Delete
#
[105] Fix | Delete
# ==== A Line at a Time
[106] Fix | Delete
#
[107] Fix | Delete
# CSV.foreach("path/to/file.csv") do |row|
[108] Fix | Delete
# # use row here...
[109] Fix | Delete
# end
[110] Fix | Delete
#
[111] Fix | Delete
# ==== All at Once
[112] Fix | Delete
#
[113] Fix | Delete
# arr_of_arrs = CSV.read("path/to/file.csv")
[114] Fix | Delete
#
[115] Fix | Delete
# === From a String
[116] Fix | Delete
#
[117] Fix | Delete
# ==== A Line at a Time
[118] Fix | Delete
#
[119] Fix | Delete
# CSV.parse("CSV,data,String") do |row|
[120] Fix | Delete
# # use row here...
[121] Fix | Delete
# end
[122] Fix | Delete
#
[123] Fix | Delete
# ==== All at Once
[124] Fix | Delete
#
[125] Fix | Delete
# arr_of_arrs = CSV.parse("CSV,data,String")
[126] Fix | Delete
#
[127] Fix | Delete
# == Writing
[128] Fix | Delete
#
[129] Fix | Delete
# === To a File
[130] Fix | Delete
#
[131] Fix | Delete
# CSV.open("path/to/file.csv", "wb") do |csv|
[132] Fix | Delete
# csv << ["row", "of", "CSV", "data"]
[133] Fix | Delete
# csv << ["another", "row"]
[134] Fix | Delete
# # ...
[135] Fix | Delete
# end
[136] Fix | Delete
#
[137] Fix | Delete
# === To a String
[138] Fix | Delete
#
[139] Fix | Delete
# csv_string = CSV.generate do |csv|
[140] Fix | Delete
# csv << ["row", "of", "CSV", "data"]
[141] Fix | Delete
# csv << ["another", "row"]
[142] Fix | Delete
# # ...
[143] Fix | Delete
# end
[144] Fix | Delete
#
[145] Fix | Delete
# == Convert a Single Line
[146] Fix | Delete
#
[147] Fix | Delete
# csv_string = ["CSV", "data"].to_csv # to CSV
[148] Fix | Delete
# csv_array = "CSV,String".parse_csv # from CSV
[149] Fix | Delete
#
[150] Fix | Delete
# == Shortcut Interface
[151] Fix | Delete
#
[152] Fix | Delete
# CSV { |csv_out| csv_out << %w{my data here} } # to $stdout
[153] Fix | Delete
# CSV(csv = "") { |csv_str| csv_str << %w{my data here} } # to a String
[154] Fix | Delete
# CSV($stderr) { |csv_err| csv_err << %w{my data here} } # to $stderr
[155] Fix | Delete
# CSV($stdin) { |csv_in| csv_in.each { |row| p row } } # from $stdin
[156] Fix | Delete
#
[157] Fix | Delete
# == Advanced Usage
[158] Fix | Delete
#
[159] Fix | Delete
# === Wrap an IO Object
[160] Fix | Delete
#
[161] Fix | Delete
# csv = CSV.new(io, options)
[162] Fix | Delete
# # ... read (with gets() or each()) from and write (with <<) to csv here ...
[163] Fix | Delete
#
[164] Fix | Delete
# == CSV and Character Encodings (M17n or Multilingualization)
[165] Fix | Delete
#
[166] Fix | Delete
# This new CSV parser is m17n savvy. The parser works in the Encoding of the IO
[167] Fix | Delete
# or String object being read from or written to. Your data is never transcoded
[168] Fix | Delete
# (unless you ask Ruby to transcode it for you) and will literally be parsed in
[169] Fix | Delete
# the Encoding it is in. Thus CSV will return Arrays or Rows of Strings in the
[170] Fix | Delete
# Encoding of your data. This is accomplished by transcoding the parser itself
[171] Fix | Delete
# into your Encoding.
[172] Fix | Delete
#
[173] Fix | Delete
# Some transcoding must take place, of course, to accomplish this multiencoding
[174] Fix | Delete
# support. For example, <tt>:col_sep</tt>, <tt>:row_sep</tt>, and
[175] Fix | Delete
# <tt>:quote_char</tt> must be transcoded to match your data. Hopefully this
[176] Fix | Delete
# makes the entire process feel transparent, since CSV's defaults should just
[177] Fix | Delete
# magically work for you data. However, you can set these values manually in
[178] Fix | Delete
# the target Encoding to avoid the translation.
[179] Fix | Delete
#
[180] Fix | Delete
# It's also important to note that while all of CSV's core parser is now
[181] Fix | Delete
# Encoding agnostic, some features are not. For example, the built-in
[182] Fix | Delete
# converters will try to transcode data to UTF-8 before making conversions.
[183] Fix | Delete
# Again, you can provide custom converters that are aware of your Encodings to
[184] Fix | Delete
# avoid this translation. It's just too hard for me to support native
[185] Fix | Delete
# conversions in all of Ruby's Encodings.
[186] Fix | Delete
#
[187] Fix | Delete
# Anyway, the practical side of this is simple: make sure IO and String objects
[188] Fix | Delete
# passed into CSV have the proper Encoding set and everything should just work.
[189] Fix | Delete
# CSV methods that allow you to open IO objects (CSV::foreach(), CSV::open(),
[190] Fix | Delete
# CSV::read(), and CSV::readlines()) do allow you to specify the Encoding.
[191] Fix | Delete
#
[192] Fix | Delete
# One minor exception comes when generating CSV into a String with an Encoding
[193] Fix | Delete
# that is not ASCII compatible. There's no existing data for CSV to use to
[194] Fix | Delete
# prepare itself and thus you will probably need to manually specify the desired
[195] Fix | Delete
# Encoding for most of those cases. It will try to guess using the fields in a
[196] Fix | Delete
# row of output though, when using CSV::generate_line() or Array#to_csv().
[197] Fix | Delete
#
[198] Fix | Delete
# I try to point out any other Encoding issues in the documentation of methods
[199] Fix | Delete
# as they come up.
[200] Fix | Delete
#
[201] Fix | Delete
# This has been tested to the best of my ability with all non-"dummy" Encodings
[202] Fix | Delete
# Ruby ships with. However, it is brave new code and may have some bugs.
[203] Fix | Delete
# Please feel free to {report}[mailto:james@grayproductions.net] any issues you
[204] Fix | Delete
# find with it.
[205] Fix | Delete
#
[206] Fix | Delete
class CSV
[207] Fix | Delete
# The version of the installed library.
[208] Fix | Delete
VERSION = "2.4.8".freeze
[209] Fix | Delete
[210] Fix | Delete
#
[211] Fix | Delete
# A CSV::Row is part Array and part Hash. It retains an order for the fields
[212] Fix | Delete
# and allows duplicates just as an Array would, but also allows you to access
[213] Fix | Delete
# fields by name just as you could if they were in a Hash.
[214] Fix | Delete
#
[215] Fix | Delete
# All rows returned by CSV will be constructed from this class, if header row
[216] Fix | Delete
# processing is activated.
[217] Fix | Delete
#
[218] Fix | Delete
class Row
[219] Fix | Delete
#
[220] Fix | Delete
# Construct a new CSV::Row from +headers+ and +fields+, which are expected
[221] Fix | Delete
# to be Arrays. If one Array is shorter than the other, it will be padded
[222] Fix | Delete
# with +nil+ objects.
[223] Fix | Delete
#
[224] Fix | Delete
# The optional +header_row+ parameter can be set to +true+ to indicate, via
[225] Fix | Delete
# CSV::Row.header_row?() and CSV::Row.field_row?(), that this is a header
[226] Fix | Delete
# row. Otherwise, the row is assumes to be a field row.
[227] Fix | Delete
#
[228] Fix | Delete
# A CSV::Row object supports the following Array methods through delegation:
[229] Fix | Delete
#
[230] Fix | Delete
# * empty?()
[231] Fix | Delete
# * length()
[232] Fix | Delete
# * size()
[233] Fix | Delete
#
[234] Fix | Delete
def initialize(headers, fields, header_row = false)
[235] Fix | Delete
@header_row = header_row
[236] Fix | Delete
headers.each { |h| h.freeze if h.is_a? String }
[237] Fix | Delete
[238] Fix | Delete
# handle extra headers or fields
[239] Fix | Delete
@row = if headers.size >= fields.size
[240] Fix | Delete
headers.zip(fields)
[241] Fix | Delete
else
[242] Fix | Delete
fields.zip(headers).map { |pair| pair.reverse! }
[243] Fix | Delete
end
[244] Fix | Delete
end
[245] Fix | Delete
[246] Fix | Delete
# Internal data format used to compare equality.
[247] Fix | Delete
attr_reader :row
[248] Fix | Delete
protected :row
[249] Fix | Delete
[250] Fix | Delete
### Array Delegation ###
[251] Fix | Delete
[252] Fix | Delete
extend Forwardable
[253] Fix | Delete
def_delegators :@row, :empty?, :length, :size
[254] Fix | Delete
[255] Fix | Delete
# Returns +true+ if this is a header row.
[256] Fix | Delete
def header_row?
[257] Fix | Delete
@header_row
[258] Fix | Delete
end
[259] Fix | Delete
[260] Fix | Delete
# Returns +true+ if this is a field row.
[261] Fix | Delete
def field_row?
[262] Fix | Delete
not header_row?
[263] Fix | Delete
end
[264] Fix | Delete
[265] Fix | Delete
# Returns the headers of this row.
[266] Fix | Delete
def headers
[267] Fix | Delete
@row.map { |pair| pair.first }
[268] Fix | Delete
end
[269] Fix | Delete
[270] Fix | Delete
#
[271] Fix | Delete
# :call-seq:
[272] Fix | Delete
# field( header )
[273] Fix | Delete
# field( header, offset )
[274] Fix | Delete
# field( index )
[275] Fix | Delete
#
[276] Fix | Delete
# This method will return the field value by +header+ or +index+. If a field
[277] Fix | Delete
# is not found, +nil+ is returned.
[278] Fix | Delete
#
[279] Fix | Delete
# When provided, +offset+ ensures that a header match occurs on or later
[280] Fix | Delete
# than the +offset+ index. You can use this to find duplicate headers,
[281] Fix | Delete
# without resorting to hard-coding exact indices.
[282] Fix | Delete
#
[283] Fix | Delete
def field(header_or_index, minimum_index = 0)
[284] Fix | Delete
# locate the pair
[285] Fix | Delete
finder = header_or_index.is_a?(Integer) ? :[] : :assoc
[286] Fix | Delete
pair = @row[minimum_index..-1].send(finder, header_or_index)
[287] Fix | Delete
[288] Fix | Delete
# return the field if we have a pair
[289] Fix | Delete
pair.nil? ? nil : pair.last
[290] Fix | Delete
end
[291] Fix | Delete
alias_method :[], :field
[292] Fix | Delete
[293] Fix | Delete
#
[294] Fix | Delete
# :call-seq:
[295] Fix | Delete
# fetch( header )
[296] Fix | Delete
# fetch( header ) { |row| ... }
[297] Fix | Delete
# fetch( header, default )
[298] Fix | Delete
#
[299] Fix | Delete
# This method will fetch the field value by +header+. It has the same
[300] Fix | Delete
# behavior as Hash#fetch: if there is a field with the given +header+, its
[301] Fix | Delete
# value is returned. Otherwise, if a block is given, it is yielded the
[302] Fix | Delete
# +header+ and its result is returned; if a +default+ is given as the
[303] Fix | Delete
# second argument, it is returned; otherwise a KeyError is raised.
[304] Fix | Delete
#
[305] Fix | Delete
def fetch(header, *varargs)
[306] Fix | Delete
raise ArgumentError, "Too many arguments" if varargs.length > 1
[307] Fix | Delete
pair = @row.assoc(header)
[308] Fix | Delete
if pair
[309] Fix | Delete
pair.last
[310] Fix | Delete
else
[311] Fix | Delete
if block_given?
[312] Fix | Delete
yield header
[313] Fix | Delete
elsif varargs.empty?
[314] Fix | Delete
raise KeyError, "key not found: #{header}"
[315] Fix | Delete
else
[316] Fix | Delete
varargs.first
[317] Fix | Delete
end
[318] Fix | Delete
end
[319] Fix | Delete
end
[320] Fix | Delete
[321] Fix | Delete
# Returns +true+ if there is a field with the given +header+.
[322] Fix | Delete
def has_key?(header)
[323] Fix | Delete
!!@row.assoc(header)
[324] Fix | Delete
end
[325] Fix | Delete
alias_method :include?, :has_key?
[326] Fix | Delete
alias_method :key?, :has_key?
[327] Fix | Delete
alias_method :member?, :has_key?
[328] Fix | Delete
[329] Fix | Delete
#
[330] Fix | Delete
# :call-seq:
[331] Fix | Delete
# []=( header, value )
[332] Fix | Delete
# []=( header, offset, value )
[333] Fix | Delete
# []=( index, value )
[334] Fix | Delete
#
[335] Fix | Delete
# Looks up the field by the semantics described in CSV::Row.field() and
[336] Fix | Delete
# assigns the +value+.
[337] Fix | Delete
#
[338] Fix | Delete
# Assigning past the end of the row with an index will set all pairs between
[339] Fix | Delete
# to <tt>[nil, nil]</tt>. Assigning to an unused header appends the new
[340] Fix | Delete
# pair.
[341] Fix | Delete
#
[342] Fix | Delete
def []=(*args)
[343] Fix | Delete
value = args.pop
[344] Fix | Delete
[345] Fix | Delete
if args.first.is_a? Integer
[346] Fix | Delete
if @row[args.first].nil? # extending past the end with index
[347] Fix | Delete
@row[args.first] = [nil, value]
[348] Fix | Delete
@row.map! { |pair| pair.nil? ? [nil, nil] : pair }
[349] Fix | Delete
else # normal index assignment
[350] Fix | Delete
@row[args.first][1] = value
[351] Fix | Delete
end
[352] Fix | Delete
else
[353] Fix | Delete
index = index(*args)
[354] Fix | Delete
if index.nil? # appending a field
[355] Fix | Delete
self << [args.first, value]
[356] Fix | Delete
else # normal header assignment
[357] Fix | Delete
@row[index][1] = value
[358] Fix | Delete
end
[359] Fix | Delete
end
[360] Fix | Delete
end
[361] Fix | Delete
[362] Fix | Delete
#
[363] Fix | Delete
# :call-seq:
[364] Fix | Delete
# <<( field )
[365] Fix | Delete
# <<( header_and_field_array )
[366] Fix | Delete
# <<( header_and_field_hash )
[367] Fix | Delete
#
[368] Fix | Delete
# If a two-element Array is provided, it is assumed to be a header and field
[369] Fix | Delete
# and the pair is appended. A Hash works the same way with the key being
[370] Fix | Delete
# the header and the value being the field. Anything else is assumed to be
[371] Fix | Delete
# a lone field which is appended with a +nil+ header.
[372] Fix | Delete
#
[373] Fix | Delete
# This method returns the row for chaining.
[374] Fix | Delete
#
[375] Fix | Delete
def <<(arg)
[376] Fix | Delete
if arg.is_a?(Array) and arg.size == 2 # appending a header and name
[377] Fix | Delete
@row << arg
[378] Fix | Delete
elsif arg.is_a?(Hash) # append header and name pairs
[379] Fix | Delete
arg.each { |pair| @row << pair }
[380] Fix | Delete
else # append field value
[381] Fix | Delete
@row << [nil, arg]
[382] Fix | Delete
end
[383] Fix | Delete
[384] Fix | Delete
self # for chaining
[385] Fix | Delete
end
[386] Fix | Delete
[387] Fix | Delete
#
[388] Fix | Delete
# A shortcut for appending multiple fields. Equivalent to:
[389] Fix | Delete
#
[390] Fix | Delete
# args.each { |arg| csv_row << arg }
[391] Fix | Delete
#
[392] Fix | Delete
# This method returns the row for chaining.
[393] Fix | Delete
#
[394] Fix | Delete
def push(*args)
[395] Fix | Delete
args.each { |arg| self << arg }
[396] Fix | Delete
[397] Fix | Delete
self # for chaining
[398] Fix | Delete
end
[399] Fix | Delete
[400] Fix | Delete
#
[401] Fix | Delete
# :call-seq:
[402] Fix | Delete
# delete( header )
[403] Fix | Delete
# delete( header, offset )
[404] Fix | Delete
# delete( index )
[405] Fix | Delete
#
[406] Fix | Delete
# Used to remove a pair from the row by +header+ or +index+. The pair is
[407] Fix | Delete
# located as described in CSV::Row.field(). The deleted pair is returned,
[408] Fix | Delete
# or +nil+ if a pair could not be found.
[409] Fix | Delete
#
[410] Fix | Delete
def delete(header_or_index, minimum_index = 0)
[411] Fix | Delete
if header_or_index.is_a? Integer # by index
[412] Fix | Delete
@row.delete_at(header_or_index)
[413] Fix | Delete
elsif i = index(header_or_index, minimum_index) # by header
[414] Fix | Delete
@row.delete_at(i)
[415] Fix | Delete
else
[416] Fix | Delete
[ ]
[417] Fix | Delete
end
[418] Fix | Delete
end
[419] Fix | Delete
[420] Fix | Delete
#
[421] Fix | Delete
# The provided +block+ is passed a header and field for each pair in the row
[422] Fix | Delete
# and expected to return +true+ or +false+, depending on whether the pair
[423] Fix | Delete
# should be deleted.
[424] Fix | Delete
#
[425] Fix | Delete
# This method returns the row for chaining.
[426] Fix | Delete
#
[427] Fix | Delete
def delete_if(&block)
[428] Fix | Delete
@row.delete_if(&block)
[429] Fix | Delete
[430] Fix | Delete
self # for chaining
[431] Fix | Delete
end
[432] Fix | Delete
[433] Fix | Delete
#
[434] Fix | Delete
# This method accepts any number of arguments which can be headers, indices,
[435] Fix | Delete
# Ranges of either, or two-element Arrays containing a header and offset.
[436] Fix | Delete
# Each argument will be replaced with a field lookup as described in
[437] Fix | Delete
# CSV::Row.field().
[438] Fix | Delete
#
[439] Fix | Delete
# If called with no arguments, all fields are returned.
[440] Fix | Delete
#
[441] Fix | Delete
def fields(*headers_and_or_indices)
[442] Fix | Delete
if headers_and_or_indices.empty? # return all fields--no arguments
[443] Fix | Delete
@row.map { |pair| pair.last }
[444] Fix | Delete
else # or work like values_at()
[445] Fix | Delete
headers_and_or_indices.inject(Array.new) do |all, h_or_i|
[446] Fix | Delete
all + if h_or_i.is_a? Range
[447] Fix | Delete
index_begin = h_or_i.begin.is_a?(Integer) ? h_or_i.begin :
[448] Fix | Delete
index(h_or_i.begin)
[449] Fix | Delete
index_end = h_or_i.end.is_a?(Integer) ? h_or_i.end :
[450] Fix | Delete
index(h_or_i.end)
[451] Fix | Delete
new_range = h_or_i.exclude_end? ? (index_begin...index_end) :
[452] Fix | Delete
(index_begin..index_end)
[453] Fix | Delete
fields.values_at(new_range)
[454] Fix | Delete
else
[455] Fix | Delete
[field(*Array(h_or_i))]
[456] Fix | Delete
end
[457] Fix | Delete
end
[458] Fix | Delete
end
[459] Fix | Delete
end
[460] Fix | Delete
alias_method :values_at, :fields
[461] Fix | Delete
[462] Fix | Delete
#
[463] Fix | Delete
# :call-seq:
[464] Fix | Delete
# index( header )
[465] Fix | Delete
# index( header, offset )
[466] Fix | Delete
#
[467] Fix | Delete
# This method will return the index of a field with the provided +header+.
[468] Fix | Delete
# The +offset+ can be used to locate duplicate header names, as described in
[469] Fix | Delete
# CSV::Row.field().
[470] Fix | Delete
#
[471] Fix | Delete
def index(header, minimum_index = 0)
[472] Fix | Delete
# find the pair
[473] Fix | Delete
index = headers[minimum_index..-1].index(header)
[474] Fix | Delete
# return the index at the right offset, if we found one
[475] Fix | Delete
index.nil? ? nil : index + minimum_index
[476] Fix | Delete
end
[477] Fix | Delete
[478] Fix | Delete
# Returns +true+ if +name+ is a header for this row, and +false+ otherwise.
[479] Fix | Delete
def header?(name)
[480] Fix | Delete
headers.include? name
[481] Fix | Delete
end
[482] Fix | Delete
alias_method :include?, :header?
[483] Fix | Delete
[484] Fix | Delete
#
[485] Fix | Delete
# Returns +true+ if +data+ matches a field in this row, and +false+
[486] Fix | Delete
# otherwise.
[487] Fix | Delete
#
[488] Fix | Delete
def field?(data)
[489] Fix | Delete
fields.include? data
[490] Fix | Delete
end
[491] Fix | Delete
[492] Fix | Delete
include Enumerable
[493] Fix | Delete
[494] Fix | Delete
#
[495] Fix | Delete
# Yields each pair of the row as header and field tuples (much like
[496] Fix | Delete
# iterating over a Hash).
[497] Fix | Delete
#
[498] Fix | Delete
# Support for Enumerable.
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function