Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/caruby/helpers/roman.rb

Instance Method Summary collapse

Instance Method Details

#to_romanString

Returns the roman numeral equivalent of this integer.

Returns:

  • (String)

    the roman numeral equivalent of this integer



20
21
22
23
24
25
26
27
# File 'lib/caruby/helpers/roman.rb', line 20

def to_roman
  if self < 1 or self > 10 then raise ArgumentError.new("#{self} cannot be converted to a roman numeral in the range I-X")
  elsif self < 4 then 'I' * self
  elsif self < 6 then ('I' * (5 - self)) + 'V'
  elsif self < 9 then 'V' + ('I' * (self - 5))
  else ('I' * (10 - self)) + 'X'
  end
end