#!/usr/bin/env ruby require 'optimist' conf = Optimist::options do banner "avg < " opt :round, "Number of digits after decimal point.", :type => :int, :default => -1, :short => "-r" opt :Running, "Running average", :type => :bool, :default => false, :short => "-R" end def print_avg sum, i, round avg = sum / i.to_f if round >= 0 puts avg.round round else puts avg end end sum = 0.0 i = 0 while line = STDIN.gets sum += line.to_f i +=1 if conf[:Running] print_avg sum, i.to_f, conf[:round] end end if not conf[:Running] print_avg sum, i.to_f, conf[:round] end