Finding the maximum line length of a file in Ruby

Problem

You intend to find the length of the longest line in a text file.

Solution

You can use this simple Ruby script as template:

#!/usr/bin/env ruby
maxLength = 0
numLines = 0
#Iterate over each line in STDIN
STDIN.read.split("\n").each do |line|
    if line.length() > maxLength
        maxLength = line.length()
    end
    numLines += 1
end
#Print what we calculated
puts "Maximum line length: #{maxLength} in #{numLines} lines"