Sha256: 31d41c8cc32c6b6aab32f9de71b3d534d90d93bc71f793eee16800f59ed95506

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module BBLib

  # Converts any integer up to 1000 to a roman numeral
  def self.to_roman num
    return num.to_s if num > 1000
     roman = {1000 => 'M', 900 => 'CM', 500 => 'D', 400 => 'CD', 100 => 'C', 90 => 'XC', 50 => 'L',
              40 => 'XL', 10 => 'X', 9 => 'IX', 5 => 'V', 4 => 'IV', 3 => 'III', 2 => 'II', 1 => 'I'}
    numeral = ""
    roman.each do |n, r|
      while num >= n
        num-= n
        numeral+= r
      end
    end
    numeral
  end

  def self.string_to_roman str
    sp = str.split ' '
    sp.map do |s|
      if s.drop_symbols.to_i.to_s == s.drop_symbols && !(s =~ /\d+\.\d+/)
        s = s.sub(s.scan(/\d+/).first.to_s, BBLib.to_roman(s.to_i))
      else
        s
      end
    end.join ' '
  end


  def self.from_roman str
    sp = str.split(' ')
    (0..1000).each do |n|
      num = BBLib.to_roman n
      if !sp.select{ |i| i[/#{num}/i]}.empty?
        for i in 0..(sp.length-1)
          if sp[i].drop_symbols.upcase == num
            sp[i] = sp[i].sub(num ,n.to_s)
          end
        end
      end
    end
    sp.join ' '
  end

end

class Fixnum
  def to_roman
    BBLib.to_roman self.to_i
  end
end

class String
  def from_roman
    BBLib.from_roman self
  end

  def from_roman!
    replace self.from_roman
  end

  def to_roman
    BBLib.string_to_roman self
  end

  def to_roman!
    replace self.to_roman
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bblib-0.3.0 lib/string/roman.rb