Class: Integer

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

Overview

roman.rb – Roman numerals

Constant Summary collapse

ROMANS =
{
     1 => 'I',
     5 => 'V',
    10 => 'X',
    50 => 'L',
   100 => 'C',
   500 => 'D',
  1000 => 'M'
}
ROMANSUB =
[
  [1000, 100],
  [ 500, 100],
  [ 100,  10],
  [  50,  10],
  [  10,   1],
  [   5,   1],
  [   1,   0]
]

Instance Method Summary collapse

Instance Method Details

#romanObject

:call-seq:

int.roman()   -> str or nil

Return the roman numeral as uppercase if possible.

1994.roman          #=> "MCMXCIV"
-1.roman            #=> nil


34
35
36
37
38
39
40
41
# File 'lib/supplement/roman.rb', line 34

def roman
  return ROMANS[ self]  if ROMANS.has_key? self
  ROMANSUB.each do |cut,sub|
    return cut.roman + (self - cut).roman  if self >  cut
    return sub.roman + (self + sub).roman  if self >= cut - sub and self < cut
  end
  nil
end