■ファイルの行数を表示する

■書式

変数名 = 0
while !ファイルハンドル.eof
変数名 = ファイルハンドル.gets
変数名 = 変数名 + 1
end

■説明

ファイルの行数を表示するにはgetsで1行ずつ読み込み、変数の値を1ずつ増やします。この変数の値が行数になります。

■サンプル

#!/usr/bin/ruby -Ku
fh = open("sample.txt","r")
count = 0
while !fh.eof
txt = fh.gets
count = count + 1
end
fh.close
print count,"\n"