Complete the code to read all lines from a file named 'data.txt' into an array.
lines = File.[1]('data.txt')
The File.readlines method reads all lines from the file and returns them as an array.
Complete the code to print each line from the file 'notes.txt' using File.readlines.
File.[1]('notes.txt').each do |line| puts line end
File.readlines reads all lines into an array, which you can then iterate over with each.
Fix the error in the code to correctly read lines from 'log.txt' and print them.
lines = File.[1]('log.txt') lines.each do |line| puts line end
File.readlines returns an array of lines, so you can iterate over it with each.
Fill both blanks to create a hash with line numbers as keys and lines as values from 'input.txt'.
lines = File.[1]('input.txt') line_hash = lines.each_with_index.to_h { |[2], i| [i + 1, [2]] }
File.readlines reads all lines into an array. Using each_with_index with a block lets you create a hash with line numbers as keys.
Fill all three blanks to create a hash with uppercase line keys and their lengths as values from 'file.txt'.
lines = File.[1]('file.txt') result = lines.to_h { |[2]| [[2].upcase, [3] ] }
File.readlines reads lines into an array. Using to_h with a block creates a hash with uppercase lines as keys and their lengths as values.