Skip to content

Instantly share code, notes, and snippets.

@kerryb
Created June 2, 2016 16:55
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 kerryb/525cc57477188d09e7aedfea3265874e to your computer and use it in GitHub Desktop.
Save kerryb/525cc57477188d09e7aedfea3265874e to your computer and use it in GitHub Desktop.
class RomanNumerals
NUMBERS = {
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",
}
def convert number
amount, symbol = NUMBERS.detect {|k,v| k <= number }
amount == number ? symbol : symbol + convert(number - amount)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment