Module: RomanNumerals

Defined in:
lib/roman-numerals.rb

Class Method Summary collapse

Class Method Details

.to_decimal(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/roman-numerals.rb', line 29

def self.to_decimal(value)
  value.upcase!
  result = 0
  @base_digits.values.reverse.each do |roman|
    while value.start_with? roman
      value = value.slice(roman.length, value.length)
      result += @base_digits.key roman
    end
  end
  result
end

.to_roman(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/roman-numerals.rb', line 18

def self.to_roman(value)
  result = ''
  @base_digits.keys.reverse.each do |decimal|
    while value >= decimal
      value -= decimal
      result += @base_digits[decimal]
    end
  end
  result
end