-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

Ara.T.Howard wrote:
> hmmm.  maybe somthing simple like

Agreed.  What do you think about adding the following snippet to csv.rb?
 No need to Symbol support?

Regards,
// NaHi

class CSV
  class Table
    def Table.parse(str_or_readable, fs = nil, rs = nil, &block)
      create.parse(str_or_readable, fs, rs, &block)
    end

    def Table.create(*field)
      new(field)
    end

    def parse(str_or_readable, fs = nil, rs = nil, &block)
      begin
        reader = CSV::Reader.create(str_or_readable, fs, rs)
        if @field.nil?
          @field = reader.shift
        end
        if block_given?
          reader.each do |row|
            yield(NamedRow.new(@field, row))
          end
          nil
        else
          reader.collect { |row|
            NamedRow.new(@field, row)
          }
        end
      ensure
        if reader
          reader.close
        end
      end
    end

    class << self
      private :new
    end

    def initialize(field)
      @field = field.empty? ? nil : field
    end

    class NamedRow < Array
      def initialize(field, row)
        super(row)
        @field = field.map { |name|
          name.to_s
        }
      end

      def [](name)
        if name.respond_to?(:id2name)
          name = name.id2name
        end
        if @field.include?(name)
          super(@field.index(name))
        else
          super
        end
      end
    end
  end
end
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Cygwin)

iD8DBQFDZwMsf6b33ts2dPkRAo6UAJ4nJvSgFz9Rj7k6hJHIVwnwiVCyRgCgwFUE
uTRzHYSR6B2NFCekZzHbVT8=
=Q8aH
-----END PGP SIGNATURE-----