Class: Converter

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

Instance Method Summary collapse

Instance Method Details

#convert(input) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/romanumerator.rb', line 21

def convert(input)
  adder = Adder.new
  numerals = input.upcase.scan /I|V|U|X|L|C|D|M/
  running_total = 0
  index = 0

  while index < numerals.length do
    s1 = adder.count(numerals[index])
    if (index + 1) < numerals.length then
      s2 = adder.count(numerals[index + 1])

      if s1 >= s2 then
        running_total += s1
        index += 1
      else
        running_total += s2 - s1
        index += 2
      end
    else
      running_total += s1
      index += 1
    end
  end
  
  running_total
end