Sha256: 502c3231fc7dd7035d6c74f37bf2c815943b2486cdd6b0aa9a63e8e060f88554

Contents?: true

Size: 936 Bytes

Versions: 1

Compression:

Stored size: 936 Bytes

Contents

# frozen_string_literal: true

require_relative 'roti/version'
require_relative 'roti/cli'

module Roti
  class Error < StandardError; end

  class << self
    CHARACTOR_LIST = ('!'..'~').to_a
    CHARACTOR_LIST_SIZE = CHARACTOR_LIST.size

    def encode(string)
      string.chars.map.with_index(1) { |charactor, index| enecode_charactor(charactor, index) }.join
    end

    def decode(string)
      string.chars.map.with_index(1) { |charactor, index| decode_charactor(charactor, index) }.join
    end

    private

    def enecode_charactor(charactor, index)
      return charactor unless CHARACTOR_LIST.include?(charactor)

      CHARACTOR_LIST[(CHARACTOR_LIST.index(charactor) + index) % CHARACTOR_LIST_SIZE]
    end

    def decode_charactor(charactor, index)
      return charactor unless CHARACTOR_LIST.include?(charactor)

      CHARACTOR_LIST[(CHARACTOR_LIST.index(charactor) - index) % CHARACTOR_LIST_SIZE]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roti-0.1.0 lib/roti.rb