main
 1#!/usr/bin/env ruby
 2
 3PLAIN_ALPHABET = ('A'..'Z').to_a
 4
 5def cipher_alphabet_for(key)
 6  PLAIN_ALPHABET.rotate(key)
 7end
 8
 9def decode(ciphertext, key)
10  mapping = Hash[cipher_alphabet_for(key).zip(PLAIN_ALPHABET)]
11  ciphertext.chars.map { |x| mapping[x] || ' ' }.join
12end
13
14key = ENV.fetch("KEY", 5).to_i
15ciphertext = ENV.fetch("CIPHERTEXT", "RTAJ TZY FY IFBS")
16
17width = (PLAIN_ALPHABET.size*2)-1
18puts "# Caesar Cipher"
19puts ""
20puts "| --------------- | --------------------------------------------------- |"
21puts "| Plain Alphabet  | #{PLAIN_ALPHABET.join(",").ljust(width)} |"
22puts "| Cipher Alphabet | #{cipher_alphabet_for(key).join(",").ljust(width)} |"
23puts "| --------------- | --------------------------------------------------- |"
24puts "| Key             | #{key.to_s.ljust(width)} |"
25puts "| Ciphertext      | #{ciphertext.to_s.ljust(width)} |"
26puts "| Plaintext       | #{decode(ciphertext, key).to_s.ljust(width)} |"