テキストファイルの内容をソートするプログラム その2

その2では、検索機能を付けてみたいと思います。
やり方は、ファイルに書き込んでいる部分を、if文で囲むだけです。


temp_lines.each do |line|
if line.match(/irb/)
write_file.puts line
end
end
ここでは、"irb"という文字列が含まれている行のみを書き込んでいます。
もちろん、読み込み時にif文を書いても構いません。


file_path = ARGV[0]

read_file = open(file_path)
write_file = open("result_" + file_path, "w")

temp_lines = []
read_file.each do |line|
temp_lines << line
end

temp_lines.sort!

temp_lines.each do |line|
if line.match(/irb/)
write_file.puts line
end
end

read_file.close
write_file.close