Ruby Flare

Your awesome Tagline

4 notes

CSV and FasterCSV with Ruby 1.8 and 1.9

In upgrading a Rails 2.3 app from Ruby 1.8 to 1.9, I had to cope with FasterCSV becoming the new CSV library. It simply meant replacing all references I had to FasterCSV with just CSV. This works well but I wanted to maintain backwards compatibility with Ruby 1.8 (at least for the short term) so I also did the following:

In my Gemfile, I instruct bundler to include the fastercsv gem only if the platform is Ruby 1.8:

gem 'fastercsv', :platforms => :mri_18

And in my config/environment.rb file, I instruct my app to treat CSV as an alias for FasterCSV when run under Ruby 1.8 (so that I can keep all my code references to CSV):

require 'csv'
if CSV.const_defined? :Reader
  # Ruby 1.8 compatible
  require 'fastercsv'
  Object.send(:remove_const, :CSV)
  CSV = FasterCSV
else
  # CSV is now FasterCSV in ruby 1.9
end

(Source: blog.grayproductions.net)

Filed under csv upgrade ruby bundler

  1. rubyflare posted this