roman.rb

Path: lib/facets/core-uncommon/facets/integer/roman.rb
Last Update: Mon Sep 12 21:11:41 +0000 2011

Methods

roman   roman?  

Public Instance methods

Considers string a Roman numeral numeral, and converts it to the corresponding integer.

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require ‘facets‘.

[Source]

# File lib/facets/core-uncommon/facets/integer/roman.rb, line 54
  def roman
    roman = upcase
    raise unless roman?
    last = roman[-1,1]
    roman.reverse.split('').inject(0) do |result, c|
      if ROMAN_VALUES[c] < ROMAN_VALUES[last]
        result -= ROMAN_VALUES[c]
      else
        last = c
        result += ROMAN_VALUES[c]
      end
    end
  end

Returns true iif the subject is a valid Roman numeral.

NOTE: This is not (presently) a common core extension and is not loaded automatically when using require ‘facets‘.

[Source]

# File lib/facets/core-uncommon/facets/integer/roman.rb, line 72
  def roman?
    ROMAN =~ upcase
  end

[Validate]