module Base26

Constants

ALPHA
VERSION

Public Class Methods

decode(value)
Alias for: to_int
encode(value)
Alias for: to_alpha
to_alpha(value) click to toggle source
# File lib/base26.rb, line 21
def self.to_alpha(value)
  raise ArgumentError, 'Value passed is not an Integer.' unless value.is_a?(Integer)
  return '' if value < 1

  quotient, rest = (value - 1).divmod(ALPHA.size)
  to_alpha(quotient) + ALPHA[rest]
end
Also aliased as: encode
to_int(value) click to toggle source
# File lib/base26.rb, line 9
def self.to_int(value)
  chars = value.downcase.split(//).reverse

  chars.each.with_index.inject(0) do |result, (char, index)|
    if (position = ALPHA.index(char)).nil?
      raise ArgumentError, 'Value not a valid Base26 String'
    end

    result + (ALPHA.size**index) * (position + 1)
  end
end
Also aliased as: decode