Skip to content

Instantly share code, notes, and snippets.

@constantine-nikolaou
Created April 15, 2016 14:47
Show Gist options
  • Save constantine-nikolaou/fa744b3fd11945671bb4720d7e308194 to your computer and use it in GitHub Desktop.
Save constantine-nikolaou/fa744b3fd11945671bb4720d7e308194 to your computer and use it in GitHub Desktop.
Solution to Caesar Cipher implemented in ruby
def alphabet
Array('a'..'z') | (Array('A'..'Z'))
end
def cs_encrypt(string, key = 0)
# Rotate switches alphabets based on the passed key
dictionary = Hash[alphabet.zip(alphabet.rotate(key))]
string.chars.map { |str| dictionary.fetch(str, ' ') }
end
string = cs_encrypt("onCe Upon a tIme there was a little house On the TOP of the hill", 3).join
puts string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment