简体   繁体   中英

How do I print out lines in a Ruby file that contains TAB character?

I have two files, "test1.rb", "test2.rb". "test1.rb" contains TAB ( "\\t" ) characters at lines 82 and 84.

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM