I must implement a simple Caesar cipher class. Initializer takes two parameters - number of positions to shift the alphabet (integer) and the alphabet used (as string, not necessarily the standard English alphabet). The class must have two instance methods encrypt and decrypt, which take a string and do the respective action based on the parameters of the initializer. These are my rspec tests:describe Caesar do  latin_encrypter = Caesar.new 4  dna_encrypter = Caesar.new 3, 'ACTG'  it 'encrypts correctly' do    expect(latin_encrypter.encrypt 'hello').to eq lipps  end  it 'encrypts correctly using DNA alphabet' do    expect(dna_encrypter.encrypt 'ACCTGA').to eq GAACTG  end  it 'decrypts correctly' do    expect(latin_encrypter.decrypt 'lipps').to eq 'hello'  end  it 'decrypts correctly using DNA alphabet' do    expect(dna_encrypter.decrypt 'GAACTG').to eq 'ACCTGA'  endendand this is my implementation:class Caesar  def initialize(shift, alphabet = 'abcdefghijklmnopqrstuvwxyz')    @shift = shift % alphabet.size    @alphabet = alphabet  end  def encrypt(string)    string.chars.map { |char| @alphabet[@alphabet.index(char) + @shift - @alphabet.size] }.join  end  def decrypt(string)    string.chars.map { |char| @alphabet[@alphabet.index(char) - @shift] }.join  endendIs this an optimal algorithm for encryption/decryption or is there a better one (perhaps using gsub)?
Caesar cipher in Ruby
ruby;caesar cipher
Ruby has tr, a very efficient method for substituting one character to another. It does not error on unknown characters, like spaces etc. Using that, the Caesar class becomes:class Caesar  def initialize(shift, alphabet = ('a'..'z').to_a.join)    i = shift % alphabet.size #I like this    @decrypt = alphabet    @encrypt = alphabet[i..-1] + alphabet[0...i]  end  def encrypt(string)    string.tr(@decrypt, @encrypt)  end  def decrypt(string)    string.tr(@encrypt, @decrypt)  endend