summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xavg24
1 files changed, 16 insertions, 8 deletions
diff --git a/avg b/avg
index 5d32bb8..ac912d6 100755
--- a/avg
+++ b/avg
@@ -4,7 +4,17 @@ require 'optimist'
conf = Optimist::options do
banner "avg < <one number per line>"
- opt :round, "Number of digits after decimal point.", :type => :int, :default => -1
+ 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
@@ -12,13 +22,11 @@ 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
-avg = sum/i.to_f
-
-if conf[:round] >= 0
- puts avg.round conf[:round]
-else
- puts avg
+if not conf[:Running]
+ print_avg sum, i.to_f, conf[:round]
end
-