0

I want to load a CSV file and create objects based on the data. The file has the following structure:

product code;product name;product price;units
RTF0145;Mixer;659;15
GTF4895;PC;9999;25

While loading the data I want to skip the first row with headers but I have a trouble using the {:headers => true} attribute, the method does nothing, no error is raised.

def Store.load_data(file, separator, headers = true)
  begin
    @items = []
    CSV.open(file, "r", {:col_sep => separator}, {:headers => headers}) do |csv|
      csv.each do |product|
        @items << Store.new(product["product code"], product["product name"], product["price"], product["units"])
      end
    end
  rescue
  end
end

I call the method like this:

Store.load_data("products.csv", ";")

If I use it without the headers argument everything works as expected:

  def Store.load_data(file, separator, headers = true)
    begin
      @items = []
      CSV.foreach(file, { :col_sep => separator }) do |row|
        @items << Store.new(row[0], row[1], row[2], row[3]) unless row[0] == "product code"
      end
    rescue
    end
  end
3
  • have you tried using the return_headers: false option? Commented Aug 1, 2018 at 12:46
  • 1
    Why are you passing the options as separate hashes (aka {:col_sep => separator}, {:headers => headers})? Commented Aug 1, 2018 at 12:50
  • Possible duplicate of Return CSV Headers in Ruby Commented Aug 1, 2018 at 12:58

1 Answer 1

0

The signature for the CSV.open method is:

CSV.open(filename, mode = "rb", options = Hash.new)

Therefore calling it like this:

CSV.open(file, "r", { ... }, { ... }) 

...is incorrect, and Ruby should throw an exception when you do that.

The correct way to call CSV.open with a Hash is either:

CSV.open(file, "r", { :a => b, :c => d, :e => f })

Or:

CSV.open(file, "r", :a => b, :c => d, :e => f)

So, to fix your problem the solution should be to change this:

CSV.open(file, "r", { :col_sep => separator }, { :headers => headers })

To this:

CSV.open(file, "r", { :col_sep => separator, :headers => headers })
Sign up to request clarification or add additional context in comments.

Comments

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.