如果你试图从某个以某种方式分隔的文件中提取一个部分,通常你会遵循这样的模式:
open(filename) do |f|
state = nil
while (line = f.gets)
case (state)
when nil
# Look for the line beginning with "FILE_SOURCES"
if (line.match(/^FILE_SOURCES/))
state = :sources
end
when :sources
# Stop printing if you hit something starting with "END"
if (line.match(/^END/))
state = nil
else
print line
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
您可以根据您所在文件的哪个部分从一种状态更改为另一种状态.
| 归档时间: |
|
| 查看次数: |
2986 次 |
| 最近记录: |