class Figures::German
Constants
- EXCEPTIONS
- PREFIXES
- UNITS
Attributes
number[R]
Public Class Methods
new(number)
click to toggle source
# File lib/figures/german.rb, line 28 def initialize(number) @number = number.to_i end
Public Instance Methods
parse()
click to toggle source
# File lib/figures/german.rb, line 32 def parse return 'null' if number == 0 triples = split_into_reverse_triples(number) word = triples.each_with_index.reduce('') do |result, (triple, index)| triple_word = triple_to_word(triple, index) result.prepend(triple_word) end.strip number < 0 ? "minus #{word}" : word end
Private Instance Methods
append_exponent_identifier(word, index)
click to toggle source
adds the exponent word to the triple word e.g. tausend for the second triple (index = 1)
Million for the third triple (index = 2) Milliarde for the fourth triple (index = 3) indexes => PREFIXES index 2,3 => 1; 4,5 => 2; 6,7 => 3; ... : floored division by 2
etc.
# File lib/figures/german.rb, line 110 def append_exponent_identifier(word, index) return word if word.empty? || index.zero? || triples_count == 1 if index == 1 word + PREFIXES[:units][0] elsif index.even? pluralize_if_plural(word + ' ' + (PREFIXES[:units][index / 2] + "llion ").capitalize) elsif index.odd? pluralize_if_plural(word + ' ' + (PREFIXES[:units][index / 2] + "lliarde ").capitalize) end end
apply_exceptions(word)
click to toggle source
replaces all exceptions in the number word
# File lib/figures/german.rb, line 128 def apply_exceptions(word) EXCEPTIONS.each do |exception, replacement| word.sub!(exception, replacement) end word end
copula(unit_digit, ten_digit)
click to toggle source
returns the copula between unit position and tens
# File lib/figures/german.rb, line 81 def copula(unit_digit, ten_digit) 'und' if ten_digit > 1 && !unit_digit.zero? end
hundred(digit)
click to toggle source
returns the word for the given hundreds number
# File lib/figures/german.rb, line 95 def hundred(digit) case digit when 0 then '' else unit(digit) + 'hundert' end end
pluralize_if_plural(word)
click to toggle source
pluralizes exponent identifiers
# File lib/figures/german.rb, line 123 def pluralize_if_plural(word) word =~ /^eins / ? word : word.sub(/e? $/, 'en ') end
split_into_reverse_triples(number)
click to toggle source
# File lib/figures/german.rb, line 51 def split_into_reverse_triples(number) @reverse_triples ||= number.abs.to_s.reverse.scan(/.{1,3}/).map(&:reverse) end
split_triple(triple)
click to toggle source
splits up a triple into hundreds, tens and unit position
# File lib/figures/german.rb, line 70 def split_triple(triple) triple.match(/\A(\d)??(\d)??(\d)\z/).captures.map(&:to_i) end
ten(digit)
click to toggle source
returns the word for the given tens digit
# File lib/figures/german.rb, line 86 def ten(digit) case digit when 0 then '' when 1 then 'zehn' else unit(digit) + 'zig' end end
triple_to_word(triple, triple_index)
click to toggle source
# File lib/figures/german.rb, line 55 def triple_to_word(triple, triple_index) hundred_digit, ten_digit, unit_digit = split_triple(triple) word = [ hundred(hundred_digit), unit(unit_digit), copula(unit_digit, ten_digit), ten(ten_digit) ].join word = append_exponent_identifier(word, triple_index) apply_exceptions(word) end
triples_count()
click to toggle source
# File lib/figures/german.rb, line 47 def triples_count @triples_count ||= split_into_reverse_triples(number).count end
unit(digit)
click to toggle source
returns the word for the given unit number
# File lib/figures/german.rb, line 75 def unit(digit) return '' if digit.zero? UNITS[digit - 1] end