Answers for "ruby file each line"

1

ruby get line from a file

# 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`
Posted by: Guest on April-20-2022
1

ruby get line from a file

# 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`
Posted by: Guest on April-20-2022
0

ruby get line from a file

# 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
Posted by: Guest on April-20-2022
-1

ruby read file line by line

File.readlines('foo').each do |line|
	puts line
end
Posted by: Guest on January-06-2022

Browse Popular Code Answers by Language