Hi,
James Edward Gray II 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"
=> #<File:example.csv (closed)>
# 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 can understand your frustration about this point. When I wrote csv.rb
at first, I thought all csv users would do the following when I define
reader style.
CSV.open("filename.csv", "r") do |reader|
reader.each do |row|
...do something...
end
end
Why don't we just write like this;
CSV.open("filename.csv", "r") do |row|
...do something...
end
I know you are considering that IO-ish methods are important. But I
don't think CSV object should handle IO methods like fcntl, fileno,
seek, tell, tty?, and so on. Would you please tell me typical and
pragmatic examples of reader style, except 'each'?
I'm confused about why CSV does this, since it offers the foreach()
method, which normally fills this role.
foreach and readlines are added recently from IO. Now I think it was a
bad choice though...
Other CSV oddities (my opinion):
Thanks!
* I always have to think, "Now do I want the *_line() method or the
*_row() method here..."
Users don't need to use *_line and *_row methods I think. When do you
use generate_line?
* Most methods take a field separator and a row separator, but
foreach() and readlines() only take the row separator.
See IO.foreach and IO.readlines. But as I wrote above, CSV should not
have these methods...
* I have to set a field separator when I really just want to set a row
separator.
csv.rb in svn repository supports pseudo-keyword-like-method-argument
style. I'll merge it ruby's csv repository before the next release.
http://dev.ctor.org/csv/browser/trunk/lib/csv.rb
# I defined keywords :fs and :rs but it should be :col_sep and :row_sep
# in conformity with faster_csv.
* A method called "generate_line()" doesn't involve a line ending.
Do not use it.
At least users rarely use it I think.
I hope that
csv.rb's open + read + block does not work as you expected
is the only and the big frustrated point of csv.rb (...if csv.rb is
enough faster 
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:
Thanks. I'll look into this. I hope those features are pluggable into
csv.rb and other modules like DBI, spreadsheet related things, HTML
table formatters, etc. I think some of these features are table
specific, not CSV.
Regards,
// NaHi