Read TXT file in Rails

0

I have an application in Ruby on Rails and need to read a particular txt file that will be sent by the user. After sending the file, the data of the file will be displayed to the user so that it confirms if the file is correct, and if yes, then save it, the file contains letters that are the answers of a proof and each letter represents an issue, for example:

"EEDEBACABDBBADAADEADEAB"

that is:

"1 => E, 2 => E, 3 => D, 4 => E"

This file will be sent in .txt format via a form, I hope you understand!

    
asked by anonymous 09.05.2017 / 13:51

2 answers

0

First you will have to read character by character and insert into an array to be able to create your hash that auto-increments your key .

answers = []
array = "abc".each_char.map { |char| char }

array.each_with_index do |value, index|
  answers.push("#{index}" => value)
end

puts answers #[{"0"=>"a"}, {"1"=>"b"}, {"2"=>"c"}]
    
09.05.2017 / 16:47
0
respostas = []

File.open("caminho/do/arquivo", "r") do |f|
  f.each_line do |linha|
    (linha.scan /\w/).each_with_index {|val,i| respostas.push(i => val)}
  end
end
    
25.05.2017 / 17:13