Ruby Get Line From A File

[Solved] Ruby Get Line From A File | Ruby - Code Explorer | yomemimo.com
Question : ruby get line from a file

Answered by : mateusz-drewniak

# open the file
File.open('foo.txt') do |file| # iterate over each line with its index file.each_line.with_index(1) do |line, number| puts "#{number}: #{line}" end
end
# since we passed a block, the file is automatically closed after `end`

Source : | Last Update : Wed, 27 Apr 22

Question : ruby get line from a file

Answered by : mateusz-drewniak

# open the file
File.open('foo.txt') do |file| # iterate over each line file.each_line do |line| puts line end
end
# since we passed a block, the file is automatically closed after `end`

Source : | Last Update : Wed, 27 Apr 22

Question : ruby get line from a file

Answered by : mateusz-drewniak

# open the file
file = File.open('foo.txt')
# iterate over each line
file.each_line do |line| puts line
end
# close the file afterwards
file.close

Source : | Last Update : Wed, 27 Apr 22

Question : ruby read file line by line

Answered by : lior

File.readlines('foo').each do |line|	puts line
end

Source : | Last Update : Thu, 13 Jan 22

Answers related to ruby get line from a file

Code Explorer Popular Question For Ruby