class Text::Format::Roman

parameters for numbering that will control how the numbers are presented when given as [](index).

:offset

The number to add to the index in order to produce the proper index. This is because tag_text indexes from 0, not 1. This defaults to 1. Any value less than 1 will be set to 1 (because Romans did not know about zero or negative numbers).

:lower

Renders the Roman numerals in lowercase if true. Defaults to false.

:postfix

The value that will be appended to the number presented by []. Defaults to nil.

:prefix

The value that will be prepended to the number presented by []. Defaults to nil.

r1 = Text::Format::Roman.new(:postfix => ".")
puts r1[0]  # => "I."
puts r1[8]  # => "IX.

r2 = Text::Format::Roman.new(:prefix => "M.")
puts r2[0]  # => "M.I"
puts r2[8]  # => "M.IX"

r3 = Text::Format::Roman.new(:offset => 3)
puts r3[0]  # => "III"
puts r3[9]  # => "XII"

r4 = Text::Format::Roman.new(:offset => 0)
puts r4[0]  # => "I"
puts r4[8]  # => "IX"

r5 = Text::Format::Roman.new(:lower => true)
puts r5[0]  # => "i"
puts r5[8]  # => "ix"

Public Class Methods

new(options = {}) click to toggle source
    # File lib/text/format/roman.rb
 97 def initialize(options = {})
 98   @offset   = options[:offset].to_i || 1
 99   @lower    = options[:lower]       || false
100   @postfix  = options[:postfix]     || nil
101   @prefix   = options[:prefix]      || nil
102 
103   @offset   = 1 if @offset < 1
104 end

Public Instance Methods

[](index) click to toggle source
   # File lib/text/format/roman.rb
37 def [](index)
38   roman = ""
39   index += @offset
40 
41     # Do 1,000s
42   roman << "M" * (index / 1000)
43   index %= 1000
44 
45     # Do 900s
46   roman << "CM" * (index / 900)
47   index %= 900
48 
49     # Do 500s
50   roman << "D" * (index / 500)
51   index %= 500
52 
53     # Do 400s
54   roman << "CD" * (index / 400)
55   index %= 400
56 
57     # Do 100s
58   roman << "C" * (index / 100)
59   index %= 100
60 
61     # Do 90s
62   roman << "XC" * (index / 90)
63   index %= 90
64 
65     # Do 50s
66   roman << "L" * (index / 50)
67   index %= 50
68 
69     # Do 40s
70   roman << "XL" * (index / 40)
71   index %= 40
72 
73     # Do 10s
74   roman << "X" * (index / 10)
75   index %= 10
76 
77     # Do 9s
78   roman << "IX" * (index / 9)
79   index %= 9
80 
81     # Do 5s
82   roman << "V" * (index / 5)
83   index %= 5
84 
85     # Do 4s
86   roman << "IV" * (index / 4)
87   index %= 4
88 
89     # Do 1s
90   roman << "I" * index
91 
92   roman.downcase! if @lower
93 
94   "#{@prefix}#{roman}#{@postfix}"
95 end