コメントの削除 -- どう書く?org

コメントの削除
http://ja.doukaku.org/185/
やってみました


# フォルダ構成
# root/
# comment_delete.rb
# /input
# /output

# コメントパターン
# 1. #
# 2. =begin
# =end
com_s = /(#.*$)/
com_b = /(^=begin)/
com_e = /(^=end)/

# 読み込みファイル名
file_nm = 'test.rb'

# 読み込みファイルpath
input_file_path = File.expand_path(file_nm,'input')

# 出力ファイルpath
output_file_path = File.expand_path(file_nm,'output')

begin
o = File.open(output_file_path,"w")
com_flg = false
File.open(input_file_path,"r"){|f|
f.each{|row|
if com_b =~ row
com_flg = true
elsif com_e =~ row
com_flg = false
else
unless com_flg
if com_s =~ row
o << row.sub!(com_s,'')
else
o << row
end
end
end
}
}
ensure
o.close if o
end