Search code examples
rubycsvosx-mountain-lion

Create and append to CSV with ruby


So I recently had to change to OSX from Ubuntu for work and I'm having some compatibility issue with my Ruby code. I'm trying to append to a CSV and if it doesn't exist, one should be created. I've used this code in Ubuntu and it worked just fine, I have no idea what the problem is.

CSV.open("~/Documents/Endeca/file.csv","a") do |csv|
    csv << [Text,Date,Name,id]
end

When I run it now, I get an error that says "No such file or directory." I'm using ruby 2.0.0 with rvm


Solution

  • ~ is interpreted ~ literally. If you want home directory, you should expand it using File.expand_path.

    File.expand_path('~/Documents/Endeca/file.csv')
    # => "/home/falsetru/Documents/Endeca/file.csv"