Good afternoon guys, I'm having a hard time understanding how Ruby works with csv, I need to read a file and import the data from that file into a variable
I have a file named die.txt so
Id | Nome | Endereco
1 | Renato | Alameda das magnolias
Both separated by \ t
I put the | to separate just to exemplify
Then I created a class called parser in parse.rb
require 'csv'
Class Parser
attr_reader :file
def initialize(file)
begin
@file = File.open(file)
rescue Exception => exception
raise Exception, "Failed to load file#{exception}"
end
end
def parse
CSV.foreach(@file, {headers: true, header_converters: :symbol, col_sep: '\t', }) do |row|
p row[:id]
p row[:nome]
...
end
end
But when I instantiate the Parser class, I call parse method I can not return the columns the idea is to throw the records into an array or hash to save in a database later.
Can anyone give me a hint of where I'm going wrong? I've never had problems with Ruby but I'm now working with CSV Thank you for your attention.
The idea is to abstract this class to read CSV files sent by forms, and insert the data into the database