Frage

I have done several small scripting/automation projects before but this is my first time using MacRuby and I couldn't find out at all why my project is not working.

This task would have been easy to fix if it was just a plain Ruby script. However, it needs a Mac GUI so I decided to use MacRuby for its Cocoa bindings.

The app itself is a simple form that will perform a calculation based from some data from an external CSV and some text fields and then show the results of the calculation.

My problem is that this code does NOT seem to work:

@arr_from_csv = CSV.read("data.csv")

Upon building the file, I get the following error:

[...]/ruby/1.9.2/CSV.rb:1335:in `open': No such file or directory - open() failed (Errno::ENOENT)

At first, I thought that I must have put the CSV file into the wrong directory inside XCode project's folder structure. I tried putting on the same folder as the script itself (app_delegate.rb). Didn't work. I tried putting it in the Resources folder, still didn't work.

Then, I decided to just use an absolute file path. So, I changed to code into:

@arr_from_csv = CSV.read("~/data.csv")

and copied the file into my home directory. It still can't read the CSV file.

Am I missing something? I'm sorry, this is my first time using MacRuby.

Thanks.

War es hilfreich?

Lösung 2

I was able to solve my problem by using:

@arr_from_csv = CSV.read(File.expand_path("~/data.csv"))

Andere Tipps

You should change the code to "/Users/xxx/data.csv". Ruby does not expand "~" to "/Users/xxx".

Notice: xxx represents your login id.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top