1
Good afternoon guys, I’m having a little trouble 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 called given.txt like this
Id | Nome | Endereco
1 | Renato | Alameda das magnolias
Both separated by t
put the | to separate just to exemplify
So 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 introduce the Parser class, I call parse method I cannot return the columns the idea is to play the records in an array or hash to save to a database later.
Can someone give me a hint of where I’m going wrong? never had problems with Ruby but I am now in working with csv Thank you for your attention
the idea is to abstract this class to read uploaded CSV files by forms, and enter the data in the database