From: Stephen White Date: 2000-12-20T05:16:32+09:00 Subject: [ruby-talk:7711] Re: Code sample for improvement On Wed, 20 Dec 2000, Stephen White wrote: > I've played around with a few other approaches, but nothing really > seems to stand out as being a better way of doing things. Here's my next iteration: #! /usr/bin/env ruby # Given the number of seconds, convert to English description table = [ [ 31557816, 'year', 'years' ], [ 2629818, 'month', 'months' ], [ 86400, 'day', 'days' ], [ 3600, 'hour', 'hours' ], [ 60, 'min', 'mins' ], [ 1, 'sec', 'secs' ] ] result = "" time = ARGV[0].to_i table.each {|unit, sing, plur| size = time / unit if size > 0 result += (size == 1) ? "#{size} #{sing} " : "#{size} #{plur} " end time %= unit } puts result This improves the way the code is structured and takes advantage of a few more Ruby features. Any more Ruby features that could make this code nicer? Maybe generalise the code and put it into a class? -- steve@deaf.org