From: Stephen White Date: 2000-12-20T04:00:57+09:00 Subject: [ruby-talk:7705] Code sample for improvement During an idle chat with someone on IRC, they presented some fairly nasty code for printing a time interval in English. I cleaned it up a bit and converted it to Ruby. #! /usr/bin/env ruby # Given the number of seconds, convert to English description def format(size, singular, plural) if size > 0 (size == 1) ? "#{size} #{singular} " : "#{size} #{plural} " else "" end end time = ARGV[0].to_i secs = time % 60; time = time / 60 mins = time % 60; time = time / 60 hours = time % 24; time = time / 24 days = time % 30; time = time / 30 months = time % 12; time = time / 12 years = time res = "" res += format(years, 'year', 'years') res += format(months, 'month', 'months') res += format(days, 'day', 'days') res += format(hours, 'hour', 'hours') res += format(mins, 'min', 'mins') res += format(secs, 'sec', 'secs') puts res Example run: [steve@localhost steve]$ ./a 10000 2 hours 46 mins 40 secs I was interested in how the new constructs available in Ruby, like closures and co-routines, could help clean up what really is pretty ghastly code. I've played around with a few other approaches, but nothing really seems to stand out as being a better way of doing things. -- steve@deaf.org