Skip to content

Instantly share code, notes, and snippets.

@AlexAvlonitis
Last active September 27, 2021 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexAvlonitis/c9bd13f99f83d50845e158e243556f4e to your computer and use it in GitHub Desktop.
Save AlexAvlonitis/c9bd13f99f83d50845e158e243556f4e to your computer and use it in GitHub Desktop.
Ceasar's cipher in ruby
# https://github.com/alexavlonitis
# Ceasar's cipher in ruby
def cipher(word, index_shift = 13)
alphabet = ('a'..'z').to_a
alphabet_upcase = ('A'..'Z').to_a
cipher_store = {}
alphabet.each.with_index do |letter, i|
cipher_store[letter] = alphabet[(index_shift + i) - alphabet.size]
end
alphabet_upcase.each.with_index do |letter, i|
cipher_store[letter] = alphabet_upcase[(index_shift + i) - alphabet_upcase.size]
end
word.split('').map { |letter| cipher_store[letter] || ' ' }.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment