Skip to content

Instantly share code, notes, and snippets.

@max-power
Last active August 29, 2015 14:16
Show Gist options
  • Save max-power/5298e63fe20f4c6c08ba to your computer and use it in GitHub Desktop.
Save max-power/5298e63fe20f4c6c08ba to your computer and use it in GitHub Desktop.
Number to Roman
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