Sha256: 69f30bf3a9eb42288e1747d9820240b0eaaefbe13bb3a1be55443a0ad0b4a1b7

Contents?: true

Size: 768 Bytes

Versions: 2

Compression:

Stored size: 768 Bytes

Contents

require 'enumerator'

class Valerii

  ENCODE_CHARS = 
    %w( B C D F G H J K 
        M N P Q R S T V 
        W Z b c d f h j
        k m n p r x t v )
    
  DECODE_MAP = ENCODE_CHARS.to_enum(:each_with_index).inject({}) do |h,(c,i)|
    h[c] = i; h
  end

  # Encodes an integer into a string
  #
  #   Valerii.encode(1234) # => "KJ"
  #
  def self.encode(number, opts = {})
    str = number.to_s(2).reverse.scan(/.{1,5}/).map do |bits|
      ENCODE_CHARS[bits.reverse.to_i(2)]
    end.reverse.join

    return str
  end

  # Decode a string to an integer
  #
  #   Valerii.decode("KJ") # => 1234
  #
  def self.decode(string)
    string.split(//).map { |char| 
      DECODE_MAP[char] or return nil 
    }.inject(0) { |result,val| (result << 5) + val }
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
ariejan-valerii-1.1.0 lib/valerii.rb
valerii-1.1.0 lib/valerii.rb