1) $hash
Never use global variables.  Instead, you can make that a constant
inside Integer.


3)
      end
    end
    str
  end
end

str is hidden in your ends.  You need to insert some blank lines in
there.

4) You recursively call your to_words() method, which accomplishes
nothing.

5)  The 'and' in the final result is improper.


nine hundred and fifty two million one hundred thousand fifty two

v.

nine hundred and fifty two million and one hundred thousand and fifty
two

v.

nine hundred fifty two million one hundred thousand fifty two

6) 1000000 v. 1_000_000

7) num = num - ....   v. num -= ....

8) Use spacing or parentheses in your calculations:

tens = num/10*10

v.

tens = num/10 * 10

9) You don't need to recursively call your function.
10) You can make your decision tree more consistent.


class Integer
  NUMBER_TO_WORD = Hash[
    1 => "one",
    2 => "two",
    3 => "three",
    4 => "four",
    5 => "five",
    6 => "six",
    7 => "seven",
    8 => "eight",
    9 => "nine",
    10 =>"ten",
    11 => "eleven",
    12 => "twelve" ,
    13 => "thirteen",
    14 => "fourteen",
    15 => "fifteen",
    16 => "sixteen",
    17 => "seventeen",
    18 => "eighteen",
    19 => "nineteen",
    20 => "twenty",
    30 => "thirty",
    40 =>"forty",
    50 => "fifty",
    60 => "sixty",
    70 => "seventy",
    80 =>"eighty",
    90 => "ninety",
    100 => "hundred",
    1000 => "thousand",
  ]

  def hundreds_helper(num)
    string_fragments = []

    if num >= 100
      hundreds = num/100
      string_fragments << "%s hundred" % NUMBER_TO_WORD[hundreds]
      num -= hundreds*100
    end

    if num >= 20
      tens = num/10 * 10
      string_fragments << NUMBER_TO_WORD[tens]
      num -= tens
    end

    if num >= 11
      string_fragments << NUMBER_TO_WORD[num]
      num = 0
    end

    if num > 0
      string_fragments << NUMBER_TO_WORD[num]
    end

    string_fragments.join(' ');

  end


  def to_words
    string_fragment= []
    num = self

    if num >= 1_000_000
      millions = num/1_000_000
      string_fragment << "%s million" % hundreds_helper(millions)
      num -= millions*1_000_000
    end

    if num >= 1_000
      thousands = num/1_000
      string_fragment << "%s thousand" % hundreds_helper(thousands)
      num -= thousands*1_000
    end

    string_fragment << hundreds_helper(num)
    string_fragment.join(' ')

  end
end

-- 
Posted via http://www.ruby-forum.com/.