class RomanNumeral

Work with Roman numerals just like normal Integers.

Constants

MAX

The largest integer representable as a roman numerable by this module.

REGEXP

Taken from O’Reilly’s Perl Cookbook 6.23. Regular Expression Grabbag.

ROMAN_VALUES
ROMAN_VALUES_ASSOC

Public Class Methods

from_integer(int) click to toggle source
# File lib/roman.rb, line 34
def self.from_integer(int)
  #return nil if integer > MAX
  return "-#{(-int).roman}" if int < 0
  return "" if int == 0
  ROMAN_VALUES_ASSOC.each do |(i, v)|
    return(i + from_integer(int-v)) if v <= int 
  end
end
is_roman_numeral?(string) click to toggle source

Returns true if string is a roman numeral.

# File lib/roman.rb, line 59
def self.is_roman_numeral?(string)
  REGEXP =~ string
end
new(val) click to toggle source

Create a new instance from an Integer, String or other RomanNumeral.

# File lib/roman.rb, line 66
def initialize(val)  
  case val
  when String
    @i = self.class.to_integer(val)  
    @s = val.frozen? ? val : val.dup.freeze  
  when Numeric  
    @i = val.to_i  
  else  
    raise ArgumentError, 'Cannot convert %p' % val  
  end  
end
to_integer(roman) click to toggle source
# File lib/roman.rb, line 44
def self.to_integer(roman)
  #return nil unless roman_string.is_roman_numeral?
  last = roman[-1,1].upcase
  roman.reverse.split('').inject(0) do |result, c|
    c = c.upcase
    if ROMAN_VALUES[c] < ROMAN_VALUES[last]
      result -= ROMAN_VALUES[c]
    else
      last = c
      result += ROMAN_VALUES[c]
    end
  end
end

Public Instance Methods

&(o) click to toggle source

bit operators

# File lib/roman.rb, line 229
def &(o)
  self.class.new(@i & o.to_int)
end
*(o) click to toggle source
# File lib/roman.rb, line 191
def *(o)
  if Numeric === o
    self.class.new(@i * o.to_num)
  else
    a, b = o.coerce(self)
    a * b
  end
end
**(o) click to toggle source
# File lib/roman.rb, line 210
def **(o)
  if Numeric === o
    self.class.new(@i / o.to_num)
  else
    a, b = o.coerce(self)
    a / b
  end
end
+(o) click to toggle source
# File lib/roman.rb, line 171
def +(o)
  if Numeric === o
    self.class.new(@i + o.to_num)
  else
    a, b = o.coerce(self)
    a + b
  end
end
+@() click to toggle source
# File lib/roman.rb, line 162
def +@
  self
end
-(o) click to toggle source
# File lib/roman.rb, line 181
def -(o)
  if Numeric === o
    self.class.new(@i - o.to_num)
  else
    a, b = o.coerce(self)
    a - b
  end
end
-@() click to toggle source
# File lib/roman.rb, line 166
def -@
  self.class.new(-@i)
end
/(o) click to toggle source
# File lib/roman.rb, line 201
def /(o)
  if Numeric === o
    self.class.new(@i / o.to_num)
  else
    a, b = o.coerce(self)
    a / b
  end
end
<<(o) click to toggle source
# File lib/roman.rb, line 219
def <<(o)
  self.class.new(@i << o.to_int)
end
<=>(o) click to toggle source
# File lib/roman.rb, line 122
def <=>(o)  
  case o
  when Numeric
    @i <=> o.to_num
  else
    a, b = o.coerce(self)
    a <=> b
  end rescue nil  
end
==(num) click to toggle source
# File lib/roman.rb, line 112
def ==(num)
  @i == num.to_num  
end
>>(o) click to toggle source
# File lib/roman.rb, line 223
def >>(o)
  self.class.new(@i >> o.to_int)
end
between?(a, b) click to toggle source
# File lib/roman.rb, line 150
def between?(a, b)
  @i.between? a, b
end
coerce(o) click to toggle source
# File lib/roman.rb, line 117
def coerce(o)  
  [self.class.new(o.to_int), self]  
end
eql?(num) click to toggle source
# File lib/roman.rb, line 107
def eql?(num)
  self.class.equal?(num.class) && @i == num.to_i  
end
even?() click to toggle source
# File lib/roman.rb, line 146
def even?
  @i.even?
end
freeze() click to toggle source

Freeze

Calls superclass method
# File lib/roman.rb, line 238
def freeze
  to_s
  super
end
hash() click to toggle source

hash code calculation

# File lib/roman.rb, line 80
def hash
  @i.hash
end
integer?() click to toggle source
# File lib/roman.rb, line 154
def integer?
  true
end
nonzero?() click to toggle source
# File lib/roman.rb, line 138
def nonzero?
  @i.nonzero?
end
odd?() click to toggle source
# File lib/roman.rb, line 142
def odd?
  @i.odd?
end
real?() click to toggle source
# File lib/roman.rb, line 158
def real?
  true
end
to_arabic() click to toggle source
# File lib/roman.rb, line 99
def to_arabic ; @i ; end
to_i() click to toggle source
# File lib/roman.rb, line 93
def to_i ; @i ; end
to_int() click to toggle source
# File lib/roman.rb, line 96
def to_int ; @i ; end
to_num() click to toggle source
# File lib/roman.rb, line 90
def to_num ; @i ; end
to_roman() click to toggle source
# File lib/roman.rb, line 102
def to_roman
  self  
end
to_s() click to toggle source
# File lib/roman.rb, line 85
def to_s
  @s ||= self.class.from_integer(@i)
end
zero?() click to toggle source

various tests that Fixnum also has

# File lib/roman.rb, line 134
def zero?
  @i.zero?
end
|(o) click to toggle source
# File lib/roman.rb, line 233
def |(o)
  self.class.new(@i | o.to_int)
end