Question about .chomp with regards to reading in lines for .txt files

How would one read in a line(from lets say a .txt file) using File.open(“file.txt”).each… but without taking in the whole line? Is there a .chomp method for files?

Each line in the text file ends with a newline character ("\n" == LF). chomp removes that character.

And where would I add my .chomp method to File.open(“file.txt”) ?

Inside the .each do loop:

___.each do |line|
    print line.chomp
end

This isn’t tested. Please provide a link to the exercise so we can see what is asked for and test the code. Thank you.

Awesome thanks that’s exactly what I needed!
I simply used it for
File.open(“to_do_list.txt”).each do |line|
my_list.addToList(line.chomp)
end

Cheers

1 Like