There are multiple ways to read lines from a file. Here are three:
# 1
File.open("temp.txt", "r") do |f|
f.each_line { |l| puts l }
end
# 2
File.open("temp.txt", "r").each_line { |l| puts l }.close
# 3
File.readlines("temp.txt").each { |l| puts l }
您显示的第一种方式即使在发生错误时也将关闭文件,而其他两种则不会。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.