0

ファイルを解析し、rubyのいくつかのヘッダー文字列を削除したいと思います。

解析されるファイルは次のようになります。

---
some
text
text2
string
and
so
on
---
another string
and the other

そして削除したい

---
some
text
text2
string
and
so
on
---

この部分。

編集:私はこのようなルビーコードを書きました

file = File.open("test")
a = file.readlines
skip = 0
a.each do |line|
   if line.match("---\n")
     if skip == 1
       skip-=1
     else
       skip+=1
     end
   else
     if skip != 1
       print line 
     end
   end

end

これは機能しますが、私のコードは汚れているので、クリーンアップする必要があると思います。コードを単純化するにはどうすればよいですか?

4

1 に答える 1

0

これは簡単な作業です(ファイルが整形式であると仮定します)。

このようなもの:

lines = # read lines from file

is_header_mode = false
lines.each do |line|
   if line == '---'
     is_header_mode = !is_header_mode
   else
     puts line unless is_header_mode
   end
end
于 2012-05-27T09:05:32.997 に答える