Sha256: ec77ea411535c4911617e6f5084395883c9b02cccf22ec5d86b7a9c39c1c1a9e

Contents?: true

Size: 857 Bytes

Versions: 1

Compression:

Stored size: 857 Bytes

Contents

#!/usr/bin/env ruby

# roman.rb
# yesmar@speakeasy.net

module Roman
  ROMAN_NUMERALS = [ ['M', 1000], ['CM', 900], ['D', 500],
    ['CD', 400], ['C', 100], ['XC', 90], ['L', 50], ['XL', 40],
    ['X', 10], ['IX', 9], ['V', 5], ['IV', 4], ['I', 1] ]

  # convert Integer to Roman numeral
  def to_roman
    remainder = self
    roman = ''
    for sym,sum in ROMAN_NUMERALS
      while remainder >= sum
        remainder -= sum
        roman << sym
      end
    end

    roman
  end

  # convert Roman numeral to Integer
  def Integer.from_roman(roman)
    str = roman.upcase
    sum = 0
    for entry in ROMAN_NUMERALS
      sym,num = entry[0], entry[1]
      while sym == str[0, sym.length]
        sum += num
        str.slice!(0, sym.length)
      end
    end

    sum
  end
end

raise RuntimeError, 'This library is for require only' if $0 == __FILE__

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rex-0.0.4 lib/rex/modules/roman.rb