Created
February 15, 2011 09:40
-
-
Save safarista/827319 to your computer and use it in GitHub Desktop.
Screwing up Peter Cooper's challenge of writing ROMAN numbers for the Copyright Date Auto update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Roman | |
ARABIC = {'I': 1, 'V': 5, 'X': 10, 'L': 50, | |
'C': 100, 'D': 500, 'M': 1000 } | |
def self.const_missing(name) | |
digits = [*name.to_s.upcase.chars].reverse.map{|r| ARABIC[r] or super} | |
digits.map.with_index {|d,i| digits[i.zero? ? i : i-1] > d ? -d : d}.reduce(:+) | |
end | |
end | |
Roman::XLVII #=> 47 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment