class RomanToArabic::Converter
Attributes
numerals[R]
roman_number[R]
Public Class Methods
new(roman_number)
click to toggle source
# File lib/roman_to_arabic/converter.rb, line 3 def initialize(roman_number) @roman_number = roman_number.upcase @numerals = roman_number.chars.map(&:to_sym) end
Public Instance Methods
convert()
click to toggle source
# File lib/roman_to_arabic/converter.rb, line 12 def convert raise InvalidInputError unless valid? previous = 0 numerals.inject(0) do |result, numeral| arabic_number = DICTIONARY[numeral.to_sym] # Simply add number to the result result += arabic_number # In case number is bigger than previous (i.e. IV) # we need to subtract this number twice: # 1. we previously added this number on the line above # 2. to do actual subtraction in order to express numbers like IV or IX if arabic_number > previous result -= 2 * previous end previous = arabic_number result end end
valid?()
click to toggle source
# File lib/roman_to_arabic/converter.rb, line 8 def valid? Validator.new(roman_number).valid? end