Sha256: 8f601ade8930e1b741b5fdbbd0330a15688d1efd29b7085520d4e911de66feef
Contents?: true
Size: 1.27 KB
Versions: 4
Compression:
Stored size: 1.27 KB
Contents
require 'cousin_roman' module CousinRoman module Roman extend self def valid?(number) matches = number.match roman_regex matches and !matches[0].empty? end # Convert Roman +number+ represented as a string to an Integer. # # It is essential that +numer+ is a valid Roman numeral. # # The steps are: # # 1. Replace factors with their numeric values # starting from subtractive factors (cause they are compound). # 2. Sum this numeric values to get the final answer def convert(number) intermediate = number.dup.downcase SUBTRACTIVES.each do |factor, value| intermediate.gsub!(factor, "(#{value})") end ONES.merge(FIVES).each do |factor, value| intermediate.gsub!(factor, "(#{value})") end intermediate.scan(/\((\d*)\)/).reduce(0) do |sum, term| sum + term.first.to_i end end def to_arabian(number) clean = number.strip convert clean if valid? clean end def to_arabian!(number) clean = number.strip if valid? clean convert clean else raise TypeError, 'not a valid roman number' end end def roman_regex /^(M{0,3})(D?C{0,3}|C[DM])(L?X{0,3}|X[LC])(V?I{0,3}|I[VX])$/i end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
cousin_roman-1.0.3 | lib/cousin_roman/roman.rb |
cousin_roman-1.0.2 | lib/cousin_roman/roman.rb |
cousin_roman-1.0.1 | lib/cousin_roman/roman.rb |
cousin_roman-1.0.0 | lib/cousin_roman/roman.rb |