Manipulate .txt file and put it in an array in Ruby

3

I need to, from a .txt file that has multiple tweets (one on each line), read this file and put it in a variable in the following format:

[1, "texto da primeira linha do arquivo"], [1, "texto da segunda linha"]

I was able to read the file and print it, but I can not mount the arrays vector.

    
asked by anonymous 09.06.2015 / 19:38

1 answer

3

You can use the method readlines

tweets = IO.readlines('filename.txt')
puts tweets[0] # => "primeiro tweet"

But if you want an array identical to what you requested, do so:

tweets = IO.readlines('filename.txt').each_with_index.map do |line, line_num|
  [line_num, line]
end
puts tweets[0] # => [0, "primeiro tweet"]
    
09.06.2015 / 20:16