Skip to content

Instantly share code, notes, and snippets.

@AlexanderFisenko
Created July 13, 2015 08:11
Show Gist options
  • Save AlexanderFisenko/ec89de6d9cd0296f062f to your computer and use it in GitHub Desktop.
Save AlexanderFisenko/ec89de6d9cd0296f062f to your computer and use it in GitHub Desktop.
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