Skip to content

Instantly share code, notes, and snippets.

@joebew42
Last active December 16, 2015 05:49
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 joebew42/5387151 to your computer and use it in GitHub Desktop.
Save joebew42/5387151 to your computer and use it in GitHub Desktop.
class Fixnum
def to_roman
return '' unless self < 5000
roman_thousands(self) << roman_hundreds(self) << roman_tens(self) << roman_units(self)
end
private
def roman_units(unit)
list_unit = ['','I','II','III','IV','V','VI','VII','VIII','IX']
list_unit[unit % 10]
end
def roman_tens(ten)
return '' unless self > 9
list_ten = ['','X','XX','XXX','XL','L','LX','LXX','LXXX','XC']
index = (ten % 100) / 10
list_ten[index]
end
def roman_hundreds(hundred)
return '' unless self > 99
list_hundred = ['','C','CC','CCC','CD','D','DC','DCC','DCCC','CM']
index = (hundred % 1000) / 100
list_hundred[index]
end
def roman_thousands(thousand)
return '' unless self > 999
list_thousand = ['','M','MM','MMM','MMMM']
index = (thousand % 10000) / 1000
list_thousand[index]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment