module Romaniac::Converter

Constants

ARABIC_ROMAN

Raw table of Arabic and Roman numerals. Fills out lazily.

INVERTED_MAP
KEY_POINTS

Arabic numerals are used as key points that come in handy when the library dynamically creates new numerals.

MAP

Public Class Methods

addends_of(int) click to toggle source

@api private @example

addends_of(6969) #=> [6000, 900, 60, 9]

@param [Integer] int @return [Array] addends of int with base of 10

# File lib/romaniac/converter.rb, line 57
def addends_of(int)
  addends = []
  pad     = 0
  while int > 0
    int, elem = int.divmod(10)
    addends.unshift(10**pad * elem)
    pad += 1
  end
  addends
end
arabic_to_roman(int) click to toggle source

@api private @example

arabic_to_roman(142) #=> "CXLII"

@param [Integer] int @return [String]

# File lib/romaniac/converter.rb, line 73
def arabic_to_roman(int)
  roman = ''
  while int > 0
    point = KEY_POINTS.find { |key_point| key_point <= int }
    times, int = int.divmod(point)
    roman << MAP[point] * times
  end
  roman
end
roman_to_arabic(roman) click to toggle source

@api private Also, stores the results of calculations into the ARABIC_ROMAN hash. @example

arabic_to_roman("CXLII") #=> 142

@param [String] roman @return [Integer]

# File lib/romaniac/converter.rb, line 94
def roman_to_arabic(roman)
  arabic = 0
  r = roman.dup
  INVERTED_MAP.each do |k, v|
    while r.index(k) == 0
      arabic += v
      r.slice!(k)
    end
  end
  ARABIC_ROMAN.merge!({ arabic => roman })
  arabic
end
to_roman(int) click to toggle source

@api private

# File lib/romaniac/converter.rb, line 84
def to_roman(int)
  ARABIC_ROMAN[int]
end