Skip to content

Instantly share code, notes, and snippets.

@safarista
Created February 15, 2011 09:40
Show Gist options
  • Save safarista/827319 to your computer and use it in GitHub Desktop.
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
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