The first version, File.open will open the file, then pass it into the
block, where you try to print it (but you’re printing the file object
itself, rather than the contents of the file), then after calling the
block,
it will close the file.
The second version will open the file, then iterate over each of its
lines,
printing them out. But since you didn’t pass a block, File.open doesn’t
close the file for you, and since you aren’t doing it, it never gets
closed.
In short, neither of these are what you want. Use File.foreach (assuming
you’re trying to iterate over lines)
File.foreach “a.txt” do |i|
puts i
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.