標準入力から読んで、標準出力に書く (行番号付加)
lesson006.rb
STDIN.each_with_index do |line, i|
puts format("%.6d | %s", i+1, line)
end
実行結果
C:\>ruby c:\study\ruby\chapter003\lesson006.rb < c:\study\ruby\chapter001\lesson
003.rb
000001 | var1 = "Hello";
000002 | var2 = "World";
000003 | print var1, ", ", var2, "!", "\n";