Skip to content

Instantly share code, notes, and snippets.

@claytonflesher
Created July 24, 2015 02:35
Show Gist options
  • Save claytonflesher/c17b19b9ff8c3571991b to your computer and use it in GitHub Desktop.
Save claytonflesher/c17b19b9ff8c3571991b to your computer and use it in GitHub Desktop.
# adapted from solution found at https://codequizzes.wordpress.com/2013/10/27/converting-an-integer-to-a-roman-numeral/
class Integer
def to_roman
number = self
result = ""
roman_hash.keys.each do |divisor|
quotient, modulus = number.divmod(divisor)
result << roman_hash[divisor] * quotient
number = modulus
end
result
end
private
def roman_hash
{
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"
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment