On Thu, 21 Dec 2000, Kevin Smith wrote:

> > 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'...].

Here's another way:

  ones_names = %w(one two three four five six seven eight nine ten
                  eleven twelve thirteen fourteen fifteen sixteen
                  seventeeen eighteen nineteen)

> >  ['', 'thousand', 'million', 'billion', 'trillion'].map { |gstr|
> >    (num, group) = num.divmod (1000)

What's the difference between map and each here?

Here's my current version, taking in some of the improvements other
people have posted...

  #! /usr/bin/env ruby

  $t = {           10=>'ten',       0=>'zero',
    10=>'ten',     11=>'eleven',    1=>'one',
    20=>'twenty',  12=>'twelve',    2=>'two',
    30=>'thirty',  13=>'thirteen',  3=>'three',
    40=>'forty',   14=>'fourteen',  4=>'four',
    50=>'fifty',   15=>'fifteen',   5=>'five',
    60=>'sixty',   16=>'sixteen',   6=>'six',
    70=>'seventy', 17=>'seventeen', 7=>'seven',
    80=>'eighty',  18=>'eighteen',  8=>'eight',
    90=>'ninety',  19=>'nineteen',  9=>'nine'
  }

  def num2(num)
    $t.key?(num) ? $t[num] : $t[num/10*10]+' '+$t[num%10]
  end

  def num3(num)
    hi, lo = num/100, num%100

    if    hi > 0 && lo > 0 then "#{$t[hi]} hundred and #{num2(lo)}"
    elsif hi > 0           then "#{$t[hi]} hundred"
    else                        "#{num2(lo)}"
    end
  end

  def newnum(num)
    res = []

    ['', ' thousand', ' million', ' billion'].each {|name|
      if num%1000 != 0 then res << num3(num%1000)+name end
      num /= 1000
    }

    res.reverse.join(', ')
  end

  num = ARGV[0].to_i
  puts newnum(num)

-- 
  steve / deaf.org