1

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

3
  • Do you have all directories in the path in place? I mean, does ~/Documents/Endeca/ exist? Commented Aug 25, 2013 at 16:43
  • Just try CSV.open("file.csv", "a") ...; does that work? Commented Aug 25, 2013 at 16:45
  • ~/Documents/Endeca/ does exist Commented Aug 25, 2013 at 16:47

1 Answer 1

5

~ 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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I actually just happened upon this myself, I'll have to keep that in mind in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.