Created
July 13, 2015 08:11
-
-
Save AlexanderFisenko/ec89de6d9cd0296f062f to your computer and use it in GitHub Desktop.
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
def rot13(string) | |
a = 'a'.ord | |
z = 'z'.ord | |
capital_a = 'A'.ord | |
capital_z = 'Z'.ord | |
decyphered_string = "" | |
string.unpack('c*').each do |char| | |
if (a..z).cover?(char) | |
modified_char = (((char - a) + 13) % 26) + a | |
decyphered_string << [modified_char].pack('c*') | |
elsif (capital_a..capital_z).cover?(char) | |
modified_char = (((char - capital_a) + 13) % 26) + capital_a | |
decyphered_string << [modified_char].pack('c*') | |
else | |
decyphered_string << char | |
end | |
end | |
decyphered_string | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment