08/12/13 22:25:44
path = 'test.txt' #適当に作っといてね
outfile = 'testwrite.txt' #問答無用で上書きされるぞ
と、すると
pathファイルを開いて、全体を一気に読み込んだものを表示する
open(path, 'r'){|f| puts f.read}
pathファイルを開いて、一行読み込んで表示するのを最後まで繰り返す
open(path, 'r'){|f| f.each{|line| puts line}}
pathファイルを開いて、一行読み込んで、行番号表示して、行の内容を表示するのを最後まで繰り返す
open(path, 'r'){|f| f.each_with_index{|line,ind| printf '%3d ',ind+1;puts line}}
pathファイルを開いて、書き込み用のoutfileファイルも開いて、一行読み込んで、ファイルに行番号書き込んでから行の内容も書き込むのを最後まで繰り返す
open(path, 'r'){|f| open(outfile,'w'){|out| f.each_with_index{|line,ind| out.printf '%3d ',ind+1;out.puts line}}}