class Cryptogram::Ciphers::Caesar

Public Class Methods

new(alphabet:, shift:) click to toggle source

@param [Array, Symbol] alphabet Array of chars or presetted alphabet name @param [Integer] shift Encryption shift

Calls superclass method Cryptogram::Ciphers::BaseCipher::new
# File lib/cryptogram/ciphers/caesar.rb, line 10
def initialize(alphabet:, shift:)
  super(alphabet: alphabet)

  @mapper = ::Cryptogram::Mapper.new(alphabet: @alphabet, shift: shift)
end

Public Instance Methods

decrypt(string) click to toggle source
# File lib/cryptogram/ciphers/caesar.rb, line 20
def decrypt(string)
  process(string) { |char| @mapper.map_to_initial(char) }
end
encrypt(string) click to toggle source
# File lib/cryptogram/ciphers/caesar.rb, line 16
def encrypt(string)
  process(string) { |char| @mapper.map_to_shifted(char) }
end

Private Instance Methods

process(string, &block) click to toggle source
# File lib/cryptogram/ciphers/caesar.rb, line 26
def process(string, &block)
  raise ArgumentError unless block_given?

  string.each_char.map(&block).join
end