From: Steve Wilhelm Date: 2009-11-16T16:34:26+09:00 Subject: Re: Roman Numerals (Arrgh!) Benoit Daloze wrote: > I didn't miss it at all, it's quite cool, but very not easy to find > directly > and to explain for an homework ... > > But I admit it's quite a nice solution, not that hard to understand. > > 2009/11/11 Gennady Bystritsky I'm going to assume the assignment has long been completed, so here is how I would have written the function. Not as elegant as the inject version above, but simpler for someone new to Ruby (like myself) to understand. def roman_numeral(number) result, numerals = "", [ [1000, "M"], [900, "CM"], [500, "D"], [400, "CD"], [100, "C"], [90, "XC"], [50, "L"], [40, "XL"], [10, "X"], [9, "IX"], [5, "V"], [4, "IV"], [1, "I"] ] numerals.each do |order, roman| result << roman * (number / order) number %= order end result end -- Posted via http://www.ruby-forum.com/.