Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require csv in rails app?

I have the following in my controller:

  private
  def csv_to_array(file)
    csv = CSV::parse(File.open(file, 'r') {|f| f.read })
    fields = csv.shift
    csv.collect { |record| Hash[*fields.zip(record).flatten ] }
  end

And it throws this issue:

NameError (uninitialized constant FController::CSV):
  app/controllers/f_controller.rb:27:in `csv_to_array'
  app/controllers/f_controller.rb:9:in `import'

It's my understanding that csv is included by default in the ruby toolkit, thus not needing to be required. What's causing this issue?

like image 405
Luigi Avatar asked Nov 11 '13 20:11

Luigi


People also ask

Is CSV a gem in Ruby?

The CSV library is part of the ruby standard library; it is not a gem (i.e. a third party library).


1 Answers

It is in the standard library, but you still have to require it:

require 'csv'

This is one of the differences betweeen core and std-lib.

like image 102
mechanicalfish Avatar answered Sep 28 '22 04:09

mechanicalfish