class Text::Format::Alpha
options for numbering that will control how the letters are presented when given as [](index)
. This numbering object will only provide 26 values (“a” .. “z”) unless :wrap is true
.
:transform
-
The symbol representing the method to be called on the letter. This must be a method that does not require any arguments.
:postfix
-
The value that will be appended to the letter presented by
[]
. Defaults tonil
. :prefix
-
The value that will be prepended to the letter presented by
[]
. Defaults tonil
. :wrap
-
If
true
, then indexes will be wrapped from “z” to “a”.
a1 = Text::Format::Alpha.new(:postfix => ".") puts a1[0] # => "a." puts a1[1] # => "b. puts a1[27] # => "" a2 = Text::Format::Alpha.new(:prefix => "A.") puts a2[0] # => "A.a" puts a2[1] # => "A.b" puts a2[27] # => "" a3 = Text::Format::Alpha.new(:transform => :upcase) puts a3[0] # => "A" puts a3[1] # => "B" puts a3[27] # => "" a4 = Text::Format::Alpha.new(:wrap => true) puts a4[0] # => "a" puts a4[27] # => "b"
Public Class Methods
new(options = {})
click to toggle source
# File lib/text/format/alpha.rb 55 def initialize(options = {}) #:yields self: 56 @transform = options[:transform] || nil 57 @wrap = options[:wrap] || false 58 @postfix = options[:postfix] || nil 59 @prefix = options[:prefix] || nil 60 end
Public Instance Methods
[](index)
click to toggle source
# File lib/text/format/alpha.rb 35 def [](index) 36 if @wrap 37 ltr = (?a + (index % 26)).chr 38 elsif index.between?(0, 25) 39 ltr = (?a + index).chr 40 else 41 ltr = nil 42 end 43 44 if ltr 45 if @transform 46 "#{@prefix}#{ltr.send(transform)}#{@postfix}" 47 else 48 "#{@prefix}#{ltr}#{@postfix}" 49 end 50 else 51 "" 52 end 53 end