Skip to content

Instantly share code, notes, and snippets.

@mururu mururu/roman.rb
Created Apr 25, 2012

Embed
What would you like to do?
Shinjuku.rb #4
class Integer
def to_roman
raise if self <= 0 || self >= 4000
a = ("%04d" % self).split(//).map(&:to_i).reverse
m = { 1 => 'I', 5 => 'V', 10 => 'X', 50 => 'L', 100 => 'C', 500 => 'D', 1000 => 'M' }
(0..3).inject("") do |s,i|
i = 3 - i
s += case a[i]
when 0..3
m[10**i] * a[i]
when 4
m[10**i] *(5-a[i]) + m[5*10**i]
when 5..8
m[5*10**i] + m[10**i] * (a[i]-5)
when 9
m[10**i] + m[10**(i+1)]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.