我有两个文件,“test1.rb”,“test2.rb”。“test1.rb”"\t"
在第 82 行和第 84 行包含 TAB ( ) 字符。
我想要这样的打印输出:
test1.rb: 82 (it means that there are TAB included in the line of 82).
test1.rb: 84 (it means that there are TAB included in the line of 84).
我有两个文件,“test1.rb”,“test2.rb”。“test1.rb”"\t"
在第 82 行和第 84 行包含 TAB ( ) 字符。
我想要这样的打印输出:
test1.rb: 82 (it means that there are TAB included in the line of 82).
test1.rb: 84 (it means that there are TAB included in the line of 84).
def check_tab file
open(file){|io| io.each_line.with_index(1){|s, l| puts "#{file}: #{l}" if s =~ /\t/}}
end
%w[test1.rb test2.rb].each{|f| check_tab(f)}
我会这样做:
["test1.rb", "test2.rb"].each do |fn|
File.foreach(fn) do |li|
puts "#{ fn }: #{ $. }" if li["\t"]
end
end
$.
表示“正在读取的文件中的当前行号”。