CSV transmogrifies into FasterCSV in Ruby 1.9

Matz let Ruby 1.9 into the wild last month. One nice change (among many) is that Ruby’s painfully slow CSV library has been replaced with James Edward Gray II’s most excellent FasterCSV library. This will most likely break existing code that uses CSV.

From James’ blog:

The methods provided on the CSV object are similar, but the old CSV code used positional parameters where as the new library user a Hash argument syntax (e.g., :row_sep => "\r\n"). That’s going to trip up any non-trivial usage.

He also gave an example of how to write CSV code that interoperates with 1.8 and 1.9:

require "csv"
if CSV.const_defined? :Reader
  # use old CSV code here…
else
  # use FasterCSV style code, but with CSV class, here…
end

Yes, it’s kind of gross, so just make sure you never have to write it. If you’re going to move to 1.9, move everything and avoid the hassle.

I’m pretty excited about the performance improvements and other enhancements that have been made in 1.9. This is the first major release of Ruby since I began hacking on it back in the day. It’s a thrill to be part of a community that avoids the “machine gun” approach to releases.

Ruby

One thought on “CSV transmogrifies into FasterCSV in Ruby 1.9

  1. How hard would it be to write a wrapper class whose methods could take either a hash or a sequence of simple parameters, and then check for the presence of :Reader and either pass the parameters unchanged or convert them and pass them on?

    Off the top of my head it doesn’t seem to hard, but I could be momentarily deluded…

Comments are closed.