在 Ruby 中查找文件的最大行长度
问题
你打算查找文本文件中最长行的长度。
解决方案
你可以使用此简单的 Ruby 脚本作为模板:
max_line_length.rb
#!/usr/bin/env ruby
maxLength = 0
numLines = 0
#遍历 STDIN 中的每一行
STDIN.read.split("\n").each do |line|
if line.length() > maxLength
maxLength = line.length()
end
numLines += 1
end
#打印我们计算的结果
puts "Maximum line length: #{maxLength} in #{numLines} lines"Check out similar posts by category:
Ruby
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow