class Bijective::Instance

Public Class Methods

new(sequence) click to toggle source

Constructor initialises instance variables @sequence and @base.

@param sequence [String] set instance object variable @sequence @raise [InitializationError] if sequence parameter has duplicate

characters

@return [Instance] new object instance

# File lib/bijective.rb, line 12
def initialize sequence
  dublicate = sequence.split(//).uniq.join

  if dublicate.length != sequence.length
    raise(InitializationError, 'Sequence string must contain only unique charaters.')
  end

  @sequence = sequence
  @base = @sequence.length
end

Public Instance Methods

decode(s) click to toggle source

Decodes the given string

@param s [String] the string to decode @return [Integer] the decoded integer

# File lib/bijective.rb, line 41
def decode s
  i = 0
  s.each_char { |c| i = i * @base + @sequence.index(c) }
  i
end
encode(i) click to toggle source

Encodes the given integer

@param i [Integer] the integer to encode @return [String] the encoded string

# File lib/bijective.rb, line 27
def encode i
  return @sequence[0,1] if i == 0
  s = ''
  while i > 0
    s << @sequence[i.modulo(@base)]
    i /= @base
  end
  s.reverse
end