user984621
user984621

Reputation: 48443

FasterCSV - instead of getting the content file getting the path to file

  def csv_parsing
    require 'csv'
    csv_file_path = File.join(File.dirname(__FILE__), "csv_data.csv")
    CSV.parse(csv_file_path) do |line|
      puts line[0]
    end
  end

This is a simple example, how I try to parse CSV file. The action above is placed in controller, the file is in the project's root.

But instead of getting the data from CSV file I am getting the path to file, like:

/Users/my_mane/ruby_folder/my_project/app/controllers/csv_data.csv

Note: the file contain a real data.

Why is printed out instead of own data just the file path?

Upvotes: 0

Views: 161

Answers (2)

bender
bender

Reputation: 1428

If you use file path and not data in string, then you can read this file line by line with:

CSV.foreach(csv_file_path) do |line|
  ...
end

Upvotes: 1

Anton
Anton

Reputation: 3036

Because CSV#parse actually parses the string you passed to it, not the file from location that this string contains. What you need is CSV#read: http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html#method-c-read

Upvotes: 2

Related Questions