Created
August 20, 2014 02:04
-
-
Save pumpkincouture/d348a65e055ece88523a to your computer and use it in GitHub Desktop.
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 RomanNumerals | |
attr_reader :numerals_hash | |
def initialize | |
@numerals_hash = {1 => "I", 4 => "IV", 5 => "V", 9 => "IX", | |
10 => "X", 50 => "L", 100 => "C", | |
500 => "D", 1000 => "M"} | |
end | |
def convert(number) | |
@numerals_hash.each do |arabic, roman| | |
if arabic == number | |
return roman | |
elsif number >= 10 | |
difference = number - 10 | |
if arabic == difference | |
equivalent += "X" | |
equivalent += roman | |
return equivalent | |
end | |
elsif number >= 10 | |
equivalent = "X" + ("I" * difference) | |
elsif number >= 5 | |
difference = number - 5 | |
equiv = "V" + ("I" * difference) | |
return equiv | |
elsif number <= 3 | |
equiv = "I" * number | |
return equiv | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment