From: James Edward Gray II Date: 2006-06-05T01:42:32+09:00 Subject: Re: FasterCSV RCR? On Jun 4, 2006, at 5:30 AM, NAKAMURA, Hiroshi wrote: > James Edward Gray II wrote: >> method arguments, we could get pretty close to perfect, but CSV does >> some odd things like confuse open() with foreach() that I chose to >> avoid >> in FasterCSV. Because of that, I can't always be sure what to do >> when > > Can you please explain what are "odd"? My biggest complaint with CSV is that open() behaves "oddly" and thus defeats all my normal expectations: >> File.open("example.csv", "w") do |csv| ?> csv.puts "1,2,3" >> csv.puts "a,b,c" >> end => nil >> require "csv" => true >> # typical Ruby style reading... ?> File.open("example.csv") do |file| ?> file.each { |row| p row } >> end "1,2,3\n" "a,b,c\n" => # >> # or... ?> File.foreach("example.csv") do |row| ?> p row >> end "1,2,3\n" "a,b,c\n" => nil >> # CSV's "odd" open() method... >> CSV.open("example.csv", "r") do |row| # "r" required ?> p row # we get rows, not the file object >> end ["1", "2", "3"] ["a", "b", "c"] => nil Of course, if you open in a writing mode, you do get a file like object. It's inconsistent. I'm confused about why CSV does this, since it offers the foreach() method, which normally fills this role. Other CSV oddities (my opinion): * I always have to think, "Now do I want the *_line() method or the *_row() method here..." * Most methods take a field separator and a row separator, but foreach() and readlines() only take the row separator. * I have to set a field separator when I really just want to set a row separator. * A method called "generate_line()" doesn't involve a line ending. >> 2. We could drop compatibility and rename FasterCSV to CSV. This >> way >> people get all the good stuff where they expect it. However, this >> would > > Can you please explain what are "good"? I'll introduce those features > into csv.rb. Here's a selection of some features from my CHANGELOG that I am not aware of in CSV: * Added built-in and custom data converters. Built-in handle numbers and dates. * Added auto-discovery for :row_sep (now the default). * Added FasterCSV::filter() for easy Unix-like CSV filters. * Added support for accessing fields by headers. * Headers can have their own converters. * Headers can be skipped or returned as needed. * FasterCSV::Row allows index or header access while retaining order and allowing for duplicate headers. * :headers can now be set to an Array of headers to use. * :headers can now be set to an external CSV String of headers to use. * Provided support for the serialization of custom Ruby objects using CSV. * Added FasterCSV::instance and FasterCSV()/FCSV() shortcuts for easy output. James Edward Gray II