Kevin Smith <sent / qualitycode.com> writes: | Dan Schmidt <dfan / harmonixmusic.com> wrote: | | >def english_num (num) | > ones_names = '.one.two.three.four.five.six.seven.eight.nine.ten.eleven.twelve.thirteen.fourteen.fifteen.sixteen.seventeen.eighteen.nineteen'.split('.') | | Nice way to set up an array of strings. I'll | borrow it. It beats ['', 'one', 'two'...]. Also, | my attempt had separate arrays for ones and | teens. This makes more sense. Well, it's slow, but it was the most convenient way to set up an array that started with an empty string. It should really go outside the function, of course. | > str = "" | > if group > 0 | > (hundreds, rest) = group.divmod (100) | > str << "#{ones_names[hundreds]} hundred " if hundreds > 0 | > if rest < 20 | > str << ones_names[rest] | > else | > (tens, ones) = rest.divmod(10) | > str << "#{tens_names[tens]} #{ones_names[ones]}" | > end | > str << " #{gstr}" | > end | | Lots of Ruby tricks in here. Harder for a nuby to | understand, but very compact. For now, at least, | I like the look of my code better. Question: How | hard would it be for this code to return 'zero' | for 0? This really isn't all that tricky, except for the use of divmod to perform % and / in a single operation. Returning 'zero' is kind of a pain in this setup because I'm using map. I'd just do a test at the beginning of the function, outside the whole map. -- http://www.dfan.org