Parent

Methods

Files

Class/Module Index [+]

Quicksearch

Tins::Token

Constants

DEFAULT_ALPHABET

Attributes

bits[RW]

Public Class Methods

new(options = {}) click to toggle source

def initialize(bits: 128, length: nil, alphabet: DEFAULT_ALPHABET)

# File lib/tins/token.rb, line 6
def initialize(options = {})
  bits     = options[:bits] || 128
  length   = options[:length]
  alphabet = options[:alphabet] || DEFAULT_ALPHABET
  alphabet.size > 1 or raise ArgumentError, 'need at least 2 symbols in alphabet'
  if length
    length > 0 or raise ArgumentError, 'length has to be positive'
  else
    bits > 0 or raise ArgumentError, 'bits has to be positive'
    length = (Math.log(1 << bits) / Math.log(alphabet.size)).ceil
  end
  self.bits = (Math.log(alphabet.size ** length) / Math.log(2)).floor
  token = ''
  length.times { token << alphabet[rand(alphabet.size)] }
  super token
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.