[英]How do I print out lines in a Ruby file that contains TAB character?
I have two files, "test1.rb", "test2.rb". 我有两个文件“ test1.rb”,“ test2.rb”。 "test1.rb" contains TAB ( "\\t"
) characters at lines 82 and 84. “ test1.rb”在第82和84行包含TAB( "\\t"
)字符。
I want a print-out like this: 我想要这样的打印输出:
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)}
I'd do this: 我会这样做:
["test1.rb", "test2.rb"].each do |fn|
File.foreach(fn) do |li|
puts "#{ fn }: #{ $. }" if li["\t"]
end
end
$.
means "the current line number in the file being read". 表示“正在读取的文件中的当前行号”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.