Class: NumbersInWords::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/numbers_in_words/writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(that) ⇒ Writer

Returns a new instance of Writer.



5
6
7
# File 'lib/numbers_in_words/writer.rb', line 5

def initialize(that)
  @that = that
end

Instance Method Details

#callObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/numbers_in_words/writer.rb', line 9

def call
  length = @that.to_s.length
  output =
    if length == 3
      # e.g. 113 splits into "one hundred" and "thirteen"
      write_groups(2)

      # more than one hundred less than one googol
    elsif length < LENGTH_OF_GOOGOL
      write_groups(3)

    elsif length >= LENGTH_OF_GOOGOL
      write_googols
    end
  output.strip
end

#group_words(size) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/numbers_in_words/writer.rb', line 26

def group_words(size)
  # 1000 and over Numbers are split into groups of three
  groups = NumberGroup.groups_of @that, size
  powers = groups.keys.sort.reverse # put in descending order

  powers.each do |power|
    name = NumbersInWords::POWERS_OF_TEN[power]
    digits = groups[power]
    yield power, name, digits
  end
end