Last active
August 29, 2015 14:16
-
-
Save max-power/5298e63fe20f4c6c08ba to your computer and use it in GitHub Desktop.
Number to Roman
This file contains hidden or 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
class RomanConverter | |
Dictionary = { | |
1000 => 'M', | |
900 => 'CM', | |
500 => 'D', | |
400 => 'CD', | |
100 => 'C', | |
90 => 'XC', | |
50 => 'L', | |
40 => 'XL', | |
10 => 'X', | |
9 => 'IX', | |
5 => 'V', | |
4 => 'IV', | |
1 => 'I' | |
}.freeze | |
def self.call(num) | |
Dictionary.each_with_object('') do |(arabic, roman), result| | |
while num >= arabic | |
result << roman | |
num -= arabic | |
end | |
end | |
end | |
end | |
class Fixnum | |
def to_roman | |
RomanConverter.call(self) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment