Go Back to Redneck Programmer
© 2007 Robb Nicholson

fileinfo.rb

A basic Ruby script to give similar info you would normally get from *nix "wc" utility, e.g. lines of text, number of characters, number of words, etc.

#!/usr/bin/env ruby

if __FILE__ == $0  
  if ARGV.length < 1 then
    puts "Usage: #{$0} filename"
    exit
  end
  
  results = []
  words = 0
  chars = 0
  minline = 0
  maxline = 0
  
  filename = ARGV.first
  File.new(filename, "r").each { |line| results << line }

  puts "#{filename} has..."
  puts " -> #{results.size} lines."
  
  results.each do |line|
	chars += line.length
	words += line.split.length
	
	if line.length > maxline then
		maxline = line.length
	elsif line.length < minline then
		minline = line.length
	end
	
  end
  puts " -> #{words} words."
  puts " -> #{chars} characters."
  puts " -> #{minline} character shortest line length."
  puts " -> #{maxline} characters longest line length."
  
end