class MoneyInWords::Integer

Constants

HUNDREDS
ONES
SEPARATOR
TEENS
TENS

Public Class Methods

new(num, options={}) click to toggle source
# File lib/money_in_words/integer.rb, line 17
def initialize(num, options={})
  @num = num
  @options = options
  @article = options[:article] || :male
end

Public Instance Methods

mega(nums) click to toggle source
# File lib/money_in_words/integer.rb, line 57
def mega(nums)
  mega = %W(#{""} хиляди милионa милиарда)

  nums.each_with_index.map do |num, i|
    place = nums.length - i - 1

    # Две хиляди, не два хиляди
    num = 'две' if num == 'два' && place == 1

    num = ONES[@article][0] if num == '' && place == 0 && nums.count == 1

    if num == ONES[@article][1] && place == 1
      'хиляда'
    else
      "#{num} #{mega[place]}"
    end
  end
end
mega_join(arr, separator=SEPARATOR) click to toggle source
# File lib/money_in_words/integer.rb, line 76
def mega_join(arr, separator=SEPARATOR)
  case arr.length
  when 0
    ''
  when 1
    arr.first
  else
    if arr.last.include?(SEPARATOR)
      arr.join(" ")
    else
      arr[0...-1].join(" ") + separator + arr.last.to_s
    end
  end

end
njoin(num, separator=SEPARATOR) click to toggle source
# File lib/money_in_words/integer.rb, line 92
def njoin(num, separator=SEPARATOR)
  case num.length
  when 0
    ''
  when 1
    num.first
  else
    num[0...-1].join(" ") + separator + num.last.to_s
  end
end
numerize(triple) click to toggle source
# File lib/money_in_words/integer.rb, line 40
def numerize(triple)
  hun, ten, one = padleft!(triple, 3, 0)
  num = []
  num << HUNDREDS[hun] if hun > 0

  case ten
  when 0
    num << ONES[@article][one] if one != 0
  when 1
    num << TEENS[one]
  else
    num << TENS[ten]
    num << ONES[@article][one] if one != 0
  end
  njoin(num)
end
padleft!(a, n, x) click to toggle source
# File lib/money_in_words/integer.rb, line 32
def padleft!(a, n, x)
  a.insert(0, *Array.new([0, n-a.length].max, x))
end
split_number(num) click to toggle source
# File lib/money_in_words/integer.rb, line 36
def split_number(num)
  num.to_i.to_s.split(//).map(&:to_i).reverse.each_slice(3).to_a.map(&:reverse).reverse
end
to_words() click to toggle source
# File lib/money_in_words/integer.rb, line 23
def to_words
  groups = split_number(@num)
  groups.map!{ |h| numerize(h) }
  groups = mega(groups)
  groups.reject!(&:blank?)
  groups = mega_join(groups)
  groups.strip
end