49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/innate/roman.rb', line 49
def roman
raise 'There is no Roman representation for numbers below 1.' if self < 1
raise 'There is no Roman representation for numbers above 4999.' if self > 4999
elements = {1000 => 'M',500 => 'D',100 => 'C',50 => 'L',10 => 'X',5 => 'V',1 => 'I'}
values = [1000, 500, 100, 50, 10, 5, 1]
r = ''
int = self
values.each do |v|
while int >= v
r += elements[v]
int -= v
end
if v > 1
v.to_s[0,1] == '5' ? t = v / 5 : t = v / 10
if int >= v - t
r += elements[t] + elements[v]
int -= v - t
end
end
end
return r
end
|