Skip to content

Instantly share code, notes, and snippets.

@ahmadseleem
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadseleem/96683a779dcc078d8e1d to your computer and use it in GitHub Desktop.
Save ahmadseleem/96683a779dcc078d8e1d to your computer and use it in GitHub Desktop.
# Convert Decimals to Roman numerals...
#
module Roman
ROMAN_STEPS =
{
'M' => 1000, 'CM' => 900,
'D' => 500, 'CD' => 400,
'C' => 100, 'XC' => 90,
'L' => 50, 'XL' => 40,
'X' => 10, 'IX' => 9,
'V' => 5, 'IV' => 4,
'I' => 1
}
def to_roman
deci = self
roman = ''
ROMAN_STEPS.each do |k, v|
while deci > v - 1
roman += k
deci -= v
end
break if deci == 0
end
roman
end
end
# Extend Numeric Class
class Numeric
include Roman
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment