为了编写更简洁的代码...
IO.popen("Generate a list of files").readlines.each{ |line|
chomped_line = line.chomp
# ...
}
IO.popen("Generate a list of files").readlines.map(&:chomp)
# Example 1
File.readlines("file.txt").each{|line| line.chomp!}
# Example 2
File.readlines("file.txt").map(&:chomp)
# Example 3
File.open("file.txt", "r"){|file| file.readlines.collect{|line| line.chomp}}
IO.read("something").split($/)
$/ 是分隔符字符串。IO.read 读取后关闭文件。
我会让它更快,消耗更少的内存:
然后是:
IO.popen( "generate_lines").each_line { |line|
line.chomp!
do_something_with line
}