class RomanNumeral

Attributes

numeral[R]

Public Class Methods

new( numeral ) click to toggle source
# File lib/roman-numeral.rb, line 4
def initialize( numeral )
        @numeral = numeral
end

Public Instance Methods

convert_from() click to toggle source
# File lib/roman-numeral.rb, line 68
def convert_from
        value = ''
        decimal_number = numeral.to_i
        numerals_map.keys.reverse.each do |decimal|
                while decimal_number >= decimal
                decimal_number -= decimal
                value += numerals_map[decimal]
                end
        end
        value
end
convert_to() click to toggle source
# File lib/roman-numeral.rb, line 55
def convert_to
        value = 0
        roman_symbols = numeral.to_s.upcase
        numerals_map.values.reverse.each do |symbol|
                while roman_symbols.start_with? ( symbol )
                roman_symbols = roman_symbols.slice(symbol.length, roman_symbols.length)
                value += numerals_map.key( symbol )
                end
        end

        value
end
numerals_map() click to toggle source
# File lib/roman-numeral.rb, line 8
def numerals_map
        {
                1    => 'I',
                4    => 'IV',
                5    => 'V',
                9    => 'IX',
                10   => 'X',
                40   => 'XL',
                50   => 'L',
                90   => 'XC',
                100  => 'C',
                400  => 'CD',
                500  => 'D',
                900  => 'CM',
                1000 => 'M'
        }
end
to_decimal() click to toggle source
# File lib/roman-numeral.rb, line 35
def to_decimal
        if valid_roman?
                decimal = convert_to
        elsif valid_decimal?
                numeral       
        else
                "Invalid numeral #{numeral}"
        end
end
to_roman() click to toggle source
# File lib/roman-numeral.rb, line 45
def to_roman
        if valid_decimal?
                roman = convert_from
        elsif valid_roman?
                numeral
        else
                "Invalid numeral #{numeral}"
        end
end
valid_decimal?() click to toggle source
# File lib/roman-numeral.rb, line 31
def valid_decimal?
        !!(numeral.to_s =~ /^\d+$/)
end
valid_roman?() click to toggle source
# File lib/roman-numeral.rb, line 26
def valid_roman?
        #Will considered as valid input if input string contains roman numeral in correct order
        !!(numeral.to_s.upcase =~ /^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/) 
end