class Romaniac

Constants

ARABIC_PATTERN
DivisionError
LIMIT

The maximum possible Roman numeral.

VERSION
VERSION_FILE

The VERSION file must be in the root directory of the library.

Public Class Methods

new(int) click to toggle source
# File lib/romaniac.rb, line 23
def initialize(int)
  validate(int)
  @int   = int.to_i
  @roman = Romaniac::Converter.to_roman(@int)
end

Public Instance Methods

*(other) click to toggle source
# File lib/romaniac.rb, line 62
def *(other)
  self.class.new(@int * other.to_i)
end
+(other) click to toggle source
# File lib/romaniac.rb, line 45
def +(other)
  self.class.new(@int + other.to_i)
end
-(other) click to toggle source
# File lib/romaniac.rb, line 49
def -(other)
  self.class.new(@int - other.to_i)
end
/(other) click to toggle source
# File lib/romaniac.rb, line 53
def /(other)
  int, fraction = @int.divmod(other.to_i)
  if fraction.zero?
    self.class.new(int)
  else
    raise Romaniac::DivisionError, "quotient isn't an integer"
  end
end
<=>(other) click to toggle source
# File lib/romaniac.rb, line 41
def <=>(other)
  @int <=> other.to_i
end
inspect() click to toggle source
# File lib/romaniac.rb, line 29
def inspect
  "(Roman: #@roman)"
end
to_i() click to toggle source
# File lib/romaniac.rb, line 33
def to_i
  @int
end
to_s() click to toggle source
# File lib/romaniac.rb, line 37
def to_s
  @roman
end

Private Instance Methods

validate(int) click to toggle source
# File lib/romaniac.rb, line 68
def validate(int)
  if int.is_a?(String)
    valid_int = (int =~ ARABIC_PATTERN && (i = int.to_i) > 0 && i <= LIMIT)
    if !valid_int
      raise ArgumentError, %|invalid value for Roman(): "#{ int }"|
    else
      return
    end
  end

  if !int.is_a?(Fixnum)
    raise TypeError, "can't convert #{ int.class } into Roman"
  end

  if int <= 0
    raise RangeError, 'integer is too small to convert into Roman'
  elsif int > LIMIT
    raise RangeError, 'integer is too big to convert into Roman'
  end
end