Last active
September 27, 2021 15:05
-
-
Save AlexAvlonitis/c9bd13f99f83d50845e158e243556f4e to your computer and use it in GitHub Desktop.
Ceasar's cipher in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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